x


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.

more ▼

asked Dec 20 '11 at 06:47 AM

kmccmk9 gravatar image

kmccmk9
31 17 29 31

Assuming 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

Dec 20 '11 at 06:50 AM DaveA

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

Dec 20 '11 at 06:57 AM kmccmk9
(comments are locked)
10|3000 characters needed characters left

4 answers: sort voted first

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?

more ▼

answered Dec 21 '11 at 09:38 AM

kmccmk9 gravatar image

kmccmk9
31 17 29 31

(comments are locked)
10|3000 characters needed characters left

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

more ▼

answered Dec 20 '11 at 06:02 PM

RRMessiah gravatar image

RRMessiah
99 2 4 8

(comments are locked)
10|3000 characters needed characters left

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

more ▼

answered Dec 20 '11 at 07:15 AM

kmccmk9 gravatar image

kmccmk9
31 17 29 31

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.

Dec 20 '11 at 08:28 AM DaveA

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# programming, I just define everything before the main function or any function to make it global.

Dec 20 '11 at 04:50 PM kmccmk9

I suggest you forget about GetComponent and do it the easy way.

See the other answer...

Dec 20 '11 at 05:25 PM jahroy
(comments are locked)
10|3000 characters needed characters left

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.

more ▼

answered Dec 20 '11 at 08:41 AM

jahroy gravatar image

jahroy
3.2k 14 18 41

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x263
x153
x148

asked: Dec 20 '11 at 06:47 AM

Seen: 1937 times

Last Updated: Dec 22 '11 at 05:27 AM