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.

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.

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);

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();

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?