Search Unity

What is wrong with my script

Discussion in 'Scripting' started by zakwaddington, Jul 5, 2018.

  1. zakwaddington

    zakwaddington

    Joined:
    Jul 5, 2018
    Posts:
    1
    All compiler errors have to be fixed before you can enter playmode!
    UnityEditor.SceneView:ShowCompileErrorNotification()


    using UnityEngine;

    public class Player_collision : MonoBehaviour
    {
    void OnCollisionEnter (UnityEngine.Collision collisionInfo)
    {
    if (collisionInfo.gameObject.tag) == "obstacle"
    {
    Debug.Log("we hit an obstacle cap");
    }
    }


    }


    can someone explain what's wrong? by the way the dents are where they are meant to be
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,188
    Your if statement is wrong. Your ) is not around the "obstacle"

    And please use code tags when posting code.

    Honestly, your ide (visual studios or monodevelop) should show you where the error is.
     
    Doug_B likes this.
  3. victorML

    victorML

    Joined:
    Jul 5, 2017
    Posts:
    38
    Code (CSharp):
    1. public class Player_collision : MonoBehaviour
    2. {
    3.     void OnCollisionEnter (UnityEngine.Collision collisionInfo)
    4.     {
    5.         if (collisionInfo.gameObject.tag == "obstacle")  //<-- This parenthesis
    6.         {
    7.             Debug.Log("we hit an obstacle cap");
    8.         }
    9.     }
    10.  
    11.  
    12. }
     
  4. MaskedMouse

    MaskedMouse

    Joined:
    Jul 8, 2014
    Posts:
    1,092
    Just a tip, use the CompareTag method, it is more performant than the Equals sign.
     
    xVergilx likes this.