Search code examples
c#unity-game-enginegameobject

Unity GameObject not found


Hey guys this is my first time try to make simple game in Unity but i got stuck in movement script,

I already have script for my player called "player" script, so now i'm trying to make a script for my controller which is a touchscreen pad, like moving left, right and jump, i'm following youtube tutorial,

Even my script and the tutorial script are same, but mine is got error, it said my GameObject is not found or missing... Here's my pad script

    public class TouchPad : 
    MonoBehaviour {

    public GameObject player;

    public void OnMouseDown()
    {
     if (gameObject.name == 
    "Left")
     {
     }
     else if (gameObject.name == 
    "Right")
     {
     }
     else if (gameObject.name == 
    "Jump")
     {
     }
     }
     public void OnMouseUp()
     {
     if (gameObject.name == "Left")
     {
     }
     else if (gameObject.name == 
    "Right")
     {
     }
     else if (gameObject.name == 
    "Jump")
     {
     }
     }
     public void OnMouseDrag()
     {
     if (gameObject.name == 
    "Left")
     {
         
    player.GetComponent<player>. 
    ().Left(); <<<<<<< The 
    <player> got "Could not be 
    Found"
     }
     else if (gameObject.name == 
    "Right")
     {
     }
     else if (gameObject.name == 
   "Jump")
     {
     }
    }
    }

Solution

  • here is a fixed code. Hope it helps. Also, I've replaced your Else If's with a switch since that is more performant than Else if.

    Your problem was that you had one (.) after player>.() In your code. It should have been

    GetComponent<player>().Left();
    

    But, I decided to fix your entire code because, while it does work, it is not good for performance.

    Also, you don't need to get the GameObject the player is on. Unless you really need to.

    public class TouchPad : MonoBehaviour 
    {
        public GameObject Player; //We need to reference this in the unity inspector.
      
        private player PlayerScript;
        
        private void Awake()
        {
            if(Player != null)
            {
                PlayerScript = Player.GetComponent<player>(); //We get player from here.
            }
        }
        
        public void OnMouseDown()
        {
            switch(gameObject.name) //Switches are faster than Else if. So use them whenever possible.
            {
                case "Left": //Code here.
                break;
                
                case "Right": //Code here.
                break;
                
                case "Jump": //Code here.
                break;
                
            }
            
        }
        
        public void OnMouseUp()
        {
            switch(gameObject.name)
            {
                case "Left": //Code here.
                break;
                
                case "Right": //Code here.
                break;
                
                case "Jump": //Code here.
                break;
                
            }
        }
         
        public void OnMouseDrag()
        {
            switch(gameObject.name)
            {
                case "Left": PlayerScript.Left();
                break;
                
                case "Right": //Code here.
                break;
                
                case "Jump": //Code here.
                break;
                
            }
       }
    }