- Home /
Access functions in another script
Hello, can someone please provide a step by step tutorial on c# code on how to access functions in another script I have? I have looked at many posts and code resources but they just give me errors.
Assu$$anonymous$$g you read http://unity3d.com/support/documentation/ScriptReference/index.Accessing_Other_Game_Objects.html, then you should post your scripts here so we can see what you're doing wrong
Ok, well ok using this code I can access the other script: void Start () { GameObject femaleNude = GameObject.Find("femalenude"); FemaleNude femalenudeScript = femaleNude.GetComponent(); femalenudeScript.ChangeEnabled(false); }
But, when using the code later, in another function I get femalenudeScript is unknown error: femalenudeScript.ChangeEnabled(true);
Answer by jahroy · Dec 20, 2011 at 08:41 AM
Here is BY FAR the easiest way to do this:
Here is how to access a script named TheOtherScript that is defined in a file named TheOtherScript.cs from another scrip named TheAccessor.
TheAccessor.cs:
public TheOtherScript scriptReference;
void Update () { Debug.Log(scriptReference.getCodeWord()); }
void Start () { if ( scriptReference == null ) { Debug.Log("script reference is null: please assign in Inspector"); } }
TheOtherScript.cs:
public class OtherScript : MonoBehaviour
public string secretCodeWord = "Super Funkily Dunkily";
public string getCodeWord () { return secretCodeWord; }
Now all you have to do is follow these steps to get it to work:
- attach a TheOtherScript to an object
- attach a TheAccessor script to a different object
- select the object with the TheAccessor script attached to it (left-click)
- look in the Inspector
- observe a slot named "The Other Script"
- drag the item with the TheOtherScript attached to it onto the slot
- enjoy
You should see the code word printed in the debug console every frame.
By the way... I don't use C#, so I'm sure there are some syntax errors in there.
This is just meant to be an example.
Answer by RRMessiah · Dec 20, 2011 at 06:02 PM
Correct me if I'm wrong, but the EASIEST way to access another script's functions is if that script is of a static class with object inheritance.
Make this script and store it anywhere in your project. You don't need to attach it to a gameobject. Just leave it in a folder.
using UnityEngine;
using System.Collections;
public static class StaticClasses : object {
public static void DebugFunction()
{
Debug.Log("You called the function.");
}
}
Then, in any script you have in the entire project, you can just call that function by just doing StaticClasses.DebugFunction();
i tried that one, but why is the system tells me that
The name 'StaticClasses'does not exist in the current context
Answer by kmccmk9 · Dec 20, 2011 at 07:15 AM
Ok, well ok using this code I can access the other script:
void Start () {
GameObject femaleNude = GameObject.Find("femalenude");
FemaleNude femalenudeScript = femaleNude.GetComponent(); femalenudeScript.ChangeEnabled(false);
}
But, when using the code later, in another function I get femalenudeScript is unknown error: femalenudeScript.ChangeEnabled(true);
You have to GetComponent in each function, or better yet, set a 'global' variable in Start and then use that variable in the other functions.
Ok what is the best way to define a global variable. I tried putting public in front but that doesn't work, I tried putting public before the start function, and that also didn't work. When I do actual c# program$$anonymous$$g, I just define everything before the main function or any function to make it global.
I suggest you forget about GetComponent and do it the easy way.
See the other answer...
Answer by kmccmk9 · Dec 21, 2011 at 09:38 AM
Thank you for all the replies. Ok using this code I was able to access the script. However, when trying to access another script attached to another model, it does not work. It does not return errors but my Male Character still shows when the scene starts.
GameObject femaleNude; FemaleNude femalenudeScript; GameObject maleNude; MaleNude malenudeScript;
// Use this for initialization
void Start () {
femaleNude = GameObject.Find("femalenude");
femalenudeScript = femaleNude.GetComponent<FemaleNude>();
femalenudeScript.ChangeEnabled(false);
maleNude = GameObject.Find("malenude");
malenudeScript = maleNude.GetComponent<MaleNude>();
malenudeScript.ChangeEnabled(false);
}
I did find out the reason it isn't working. The script has to be applied to all children and not just the parent. How can I do this if I have a large amount of children?
Your answer
![](https://koobas.hobune.stream/wayback/20220613062228im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How do I call a function in another gameObject's script? 5 Answers
Methods of Accessing scripts - performance & neatness 1 Answer
Is constantly referencing or defining once then changing better 1 Answer
Having an object reference itself 2 Answers
Multiple objects with the same script having trouble accessing another script/object 1 Answer