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

Accessing a function from a 2. script

Discussion in 'Scripting' started by phil2988, Apr 4, 2014.

  1. phil2988

    phil2988

    Joined:
    Apr 11, 2013
    Posts:
    23
    Hi everybody, i was wondering how i can call a function on my gamebjects from my player?
    i want to acces an animation when my character has moven a given number of units! :confused:
     
  2. MD_Reptile

    MD_Reptile

    Joined:
    Jan 19, 2012
    Posts:
    2,664
    well if you write C#, it would be something like

    Code (csharp):
    1.  
    2. public void MakeGameObjectDoSomething(int SomeNumber)
    3. {
    4. // do magic
    5. }
    6.  
    and from your player script which has a reference to that gameobject script:

    Code (csharp):
    1.  
    2. MyGameObjectScript.MakeGameObjectDoSomething(365);
    3.  
    which should allow you to access the other scripts method/function.

    And to get that reference to your gameobjects script, just have a public variable of your scripts type, and then assign it in the inspector. Something like:

    Code (csharp):
    1.  
    2. public MyGameObjectScriptName MyGameObjectScript; // set in inspector
    3.  
     
    Last edited: Apr 4, 2014
  3. phil2988

    phil2988

    Joined:
    Apr 11, 2013
    Posts:
    23
    Okay thank you! You just saved my game! X-D
     
  4. MD_Reptile

    MD_Reptile

    Joined:
    Jan 19, 2012
    Posts:
    2,664
    No problem, glad I could help :)