Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Cant access other gameObj's function from script (C#)

Discussion in 'Scripting' started by GoshFather, May 4, 2018.

  1. GoshFather

    GoshFather

    Joined:
    Mar 15, 2015
    Posts:
    11
    I'm attempting to dynamically assign a method from a "player" object to a UI button's onClick() method once the player is instantiated. It looks like I have everything set up. However when I finally call "button.onClick.AddListener (() => {plyrHmn.plyrFunction();});" I get the following error:

    "Assets/Resources/Scripts/instPlyrHmn.cs(12,46): error CS1061: Type `UnityEngine.GameObject' does not contain a definition for `plyrFunction' and no extension method `plyrFunction' of type `UnityEngine.GameObject' could be found. Are you missing an assembly reference?"

    I'm pretty new to Unity and C#. But previously, I have been able to call methods from other objects. It just doesn't seem to work in this particular case. I am running the "Setup" script on a third-party "mediator" object. Attached are both of the scripts. Thanks instPlyrHmn.png plyrHmn.png
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,186
    You need to get the component before you can access it's methods.
    plyrHmn is a GameObject, you should be using getComponent<plyrHmn>() to get the instance of the script from it.

    Note that if this button exist in the scene, you can just as easily setup the onclick in the inspector.
     
  3. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    A GameObject isn't a plyrHmn, though it may have the plyrHmn attached, and therefore doesn't have the plyrFunction available. You need to call GetComponent<plyrHmn>() on your GameObject to get access to the actual script.
     
  4. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    It looks like you found the game object with that name, but not the script.
    You probably want:
    Code (csharp):
    1. plyrHmn.GetComponent<plyrHmn>().plyrFunction()
    I would suggest that you try to use a public variable or private with the SerializeField attribute, and drag n drop the reference in the inspector, as opposed to using GameObject.Find which is slow and error-prone.
    quick edit to echo @Brathnann 's suggestion for adding the OnClick event in the inspector, if possible.

    Check out this thread for how to post code directly into the thread, nicely formatted, so you can avoid screenshots of code in the future: https://forum.unity.com/threads/using-code-tags-properly.143875/
     
  5. GoshFather

    GoshFather

    Joined:
    Mar 15, 2015
    Posts:
    11
    Thank you guys for the quick replies. I'll be sure to include the CODE tags in the future. After solving that relatively simple issue. Now when I run the code it tells me line 11 does not reference to an instance of an object. So now the error occurs here
    Code (csharp):
    1.  Button button = GameObject.Find("UI").GetComponent<Button>();
    Sorry for sounding noobish. I've made real progress by reading the docs. However I seem to be having issues with this particular script.
     
  6. GoshFather

    GoshFather

    Joined:
    Mar 15, 2015
    Posts:
    11
    Rather, the error occurs when
    Code (csharp):
    1.  button.onClick.RemoveAllListeners ();
    is called.
     
  7. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,186
    Which means button is null. Do you have a GameObject named UI with a Button script on it?

    As mentioned, avoiding GameObject.Find is a good idea, for exact reasons such as this.
     
  8. GoshFather

    GoshFather

    Joined:
    Mar 15, 2015
    Posts:
    11
    It turns out I'm making simple mistakes from staying up all night coding. I corrected the line to
    Code (csharp):
    1.  Button button = GameObject.Find ("myButton").GetComponent<Button>();
    I had my gameObject.name confused with it's attached component of the same name lol. Goodnight guys. I'm glad to join this communtiy.