-2

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")
     {
     }
    }
    }
  • Not your current issue, but `if (gameObject.name == "Lef")` looks like a typo. Is your player class named `player` or `Player`? Did you drag the player object into the player slot in the inspector? – Retired Ninja Jun 06 '21 at 00:55
  • Oh no, i'm sorry, its not typo in my script only in here,, the problem is the GetComponent,,, you mean i need to drag the player.script to... Where? – Developer Utama Jun 06 '21 at 01:07
  • On which line is it exactly telling you the game object is missing? Is it the one this script is in? –  Jun 06 '21 at 01:10
  • "public GameObject player;" got error or missing, yes its in those script – Developer Utama Jun 06 '21 at 01:12
  • Sorry, I still don't understand. You mean unity is telling you that the game object called Player is missing? But it's not even used in your script yet. Unity won't just throw that error for a variable field. Or do you mean that the TouchPad script is missing? –  Jun 06 '21 at 01:16
  • Do you ever assign a value to "player" field? You seem to use it but it was never assigned. No wonder it gives you an error that it does not exist. – SnowGroomer Jun 06 '21 at 01:17
  • Haha yeah, even i don't know what to do with it, ```public GameObject player;``` is missing while the ```player.GotComponent().Left();``` error too because the "player" is not detected,,, i have another script called "player" its also public and already scripted for my character, so aren't ```public GameObject player;``` is for access the "player" script that i have? – Developer Utama Jun 06 '21 at 01:32
  • 1
    `player.GetComponent.().Left();` doesn't compile. Please edit the question to include a [mre]. – Ruzihm Jun 06 '21 at 02:39
  • @Ruzihm as I understand the in code comments that is exactly the issue ;) – derHugo Jun 06 '21 at 06:29

1 Answers1

1

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;
            
        }
   }
}
  • [Should you answer questions where the cause of the problem is a typo?](https://meta.stackoverflow.com/questions/366135/should-you-answer-questions-where-the-cause-of-the-problem-is-a-typo) – derHugo Jun 06 '21 at 06:28
  • Their code could be improved, so I improved it and fixed the problem. –  Jun 06 '21 at 06:29