GaMister
GaMister

Reputation: 3

Trying to make a simple Tag game in unity

I've been trying to make the code for this idea and I just can't get it. Basically when a tagged player collides with a non tagged player, the roles turn and their colors change. This is my current code. there's a lot of mistakes :

public class Tag : MonoBehaviour
{
    public Material[] material;
    Renderer rend;
    [SerializeField]
    string strTag;
    bool isTagged;

    private void OnCollisionEnter(Collision col)
    {
        if (col.gameObject.tag == strTag)
        {
           
          if(rend.sharedMaterial = material[0] && isTagged == true)
             rend.sharedMaterial = material[1];
             isTagged= false;


          if(rend.sharedMaterial = material[1] && isTagged = false)
             rend.sharedMaterial = material[0];
             isTagged= true;
        }  

    }
     private void OnCollisionExit (Collision col)
     {   
        if(col.gameObject.tag == strTag)
         {
        null;
         }
     }

    void Start()
    {
        rend = GetComponent<Renderer>();
        rend.enabled = true;
        rend.sharedMaterial = material[1];    
    }

}

Upvotes: 0

Views: 561

Answers (1)

derHugo
derHugo

Reputation: 90570

In

if(rend.sharedMaterial = material[0] && isTagged == true)
    rend.sharedMaterial = material[1];
isTagged= false;

The condition only aplies to the very first line. I changed the indentation so you can see what that means.

It basically equals doing

if(rend.sharedMaterial = material[0] && isTagged == true)
{
    rend.sharedMaterial = material[1];
}
isTagged= false;

so no matter what as soon as the first

col.gameObject.tag == strTag

is met you always set

isTagged = true;

Then after you have fixed your code and added { } around your blocks you will have a second pitfall!

After setting and assigning

rend.sharedMaterial = material[1];
isTagged= false;

You second condition

if(rend.sharedMaterial = material[1] && isTagged == false)

will automatically be true and dirctly revert the changes you just made


What you rather want to use is

 private void OnCollisionEnter(Collision col)
 {
    // In general rather use CompareTag instead of ==
    // it (is slightly faster and) adds some security since instead of failing silently
    // it throws an error if the given Tag doesn't exist 
    if (!col.gameObject.CompareTag(strTag)) return;
    
    // And then you want to wrap your multiline code blocks 
    // with brackets - otherwisethe condition only applies to the first line
    if(rend.sharedMaterial = material[0] && isTagged)
    {
        rend.sharedMaterial = material[1];
        isTagged= false;
    }
    // Here you want to use exclusive if - else if blocks!
    // You don't want to execute the second check if the first one was already met!
    else if(rend.sharedMaterial = material[1] && !isTagged)
    {
        rend.sharedMaterial = material[0];
        isTagged= true;
    }
}

Personally I would then change it a bit:

private void OnCollisionEnter(Collision col)
{
    if (!col.gameObject.CompareTag(strTag)) return;
    
    // simply invert the bool flag
    isTagged = !isTagged;
    // Depending on the isTagged flag chose the material
    // See https://learn.microsoft.com/dotnet/csharp/language-reference/operators/conditional-operator#conditional-operator-and-an-ifelse-statement
    rend.sharedMaterial = isTagged ? material[1] : material[0];
}

Upvotes: 3

Related Questions