0

I havd tried very hard to access and call a script's function from outside of the script.

----- this is inside a c# script attached to an animating sprite

    public class example : MonoBehaviour {
    void RunZebra() {
            Zebra other = GetComponent<Zebra>();
            other.RunIt();
        }
     }

------ Zebra script attached to a sprite anim

    public class Zebra : MonoBehaviour {
        public void RunIt() {
            // action starting animation

         }
     }

But it does not execute for some reason?

I need to access and set ScriptName's variable so it's animation state is changed.

Any help and I'll be a happy man.

8
  • Please post actual code here. By the little info you gave it´s only possible to guess. For example it could be that the function you want to call is declared private, or you have a typo, or many other things. Commented Sep 17, 2016 at 13:39
  • Sorry. Have added more code. Commented Sep 17, 2016 at 14:07
  • If you correctly attached the Zebra and example component to the same gameobject and called RunZebra it should work without any issue. Are you confident the method is not called? Have you tried adding a debug.log() to make sure it wasn't being called? Commented Sep 17, 2016 at 15:03
  • This part looks legit = it should execute. To check that the method "Zebra.RunIt" is called, you can put debug output into it, e. g. by 'Debug.Log("Zebra.RunIt is called")' or similar. I guess the actual problem lies in the part "// action starting animation". So it would help if you supplied that code also. Commented Sep 17, 2016 at 15:06
  • (Augure was faster ;-) ) Commented Sep 17, 2016 at 15:07

3 Answers 3

1

If the RunZebra script in your question is your complete script then your are missing something important.

Your RunZebra function must be called from somewhere. Either from the Start(), Update() function or from another script.

public class example : MonoBehaviour {

void Start()
{
    RunZebra();
}

void RunZebra() {
        Zebra other = GetComponent<Zebra>();
        other.RunIt();
    }
 }

If this does not solve your problem and Debug.Log is still not being displayed then you simply forgot to attach your example script to a GameObject. So, attach the example script to a GameObject that is enabled.

EDIT:

For the new null error, replace

GetComponent<Zebra>();

with

Zebra other = GameObject.Find("NameOfGameObjectZebraIsAttachedTo").GetComp‌​onent<Zebra>();
Sign up to request clarification or add additional context in comments.

10 Comments

I have a RunZebra(); declaration in my example script. Perhaps it is not possible when Zebra and example are attached to different gameObjects? But how then do you keep track of each "character" on the scene? Ie. a man and a zebra?
I do not understand your current question. You main problem is that RunZebra does not execute for some reason. According to your comments, even Debug.Log does not work. This is a sign that the RunZebra function is not being called anywhere. Is the Debug.Log working now?
I have dragged 2 different sprites onto the same scene. Then I have added a script to each. I have tried the Debug.Log. It now says: Object reference not set to an instance of an Object. Sorry. Am all new to Unity.
Then that is one of the problems. The next step is to replace Zebra other = GetComponent<Zebra>(); with GameObject.Find("NameOfGameObjectZebraIsAttachedTo").GetComponent<Zebra>();.
Where do I see its name? It's called Zebra - the sprite fof the zebra
|
0

I think I get the problem now. So you have some script named example attached to some GameObject man and one or more GameObjects that have the script Zebra attached to it. You want to execute the Zebra.RunIt method for each Zebra object by the RunZebra method of the man object.

To accomplish that you have to adapt the example script.

I suggest you use a public array of Zebra variables in the example script attached to the man GameObject. Then you can drag'n'drop each Zebra GameObject via the Unity3d editor UI into that array property field of the man object´s example script. (I named it ZebraGameObject below. You might have to set the Size property of the ZebraGameObject property first in the UI to the count of Zebra objects).

Then the Unity3d engine automatically gets references to each Zebra script for the example script from the previously set objects, so that the RunIt method can get called.

Nothing to change for the Zebra script assuming the code in // action starting animation is correct.

Here is the example script with the necessary adaptations.

using UnityEngine;

public class example : MonoBehaviour {

    public Zebra[] ZebraGameObject = null;

    void RunZebra() {
        if (ZebraGameObject != null){
            for (int i=0; i<ZebraGameObject.Length; i++){
                ZebraGameObject[i].RunIt();
            }
        }
    }
}

That method works great if the Zebra objects do not get spawned dynamically but are created at design time. If you need to handle dynamically created Zebra objects, then you might want to look into the GameObject.FindObjectsOfType method or maybe the GameObject.FindObjectsOfTypeAll method.

Comments

0

Thanks for the HELP!!!

I did the following to set the Var of the Zebra (so it should animate):

    GameObject objectIs = GameObject.Find("Zebra");

            //print (objectIs.name);

            Animator zanimation;
            zanimation = objectIs.GetComponent<Animator> ();
            zanimation.SetBool("zebraLaugh", true);

This did the trick. Thanks "Programmer" - You really helped!

Cheers

1 Comment

Nice. That's how you get component from another GameObject. You only use GetComponent<Animator> (); without obj.GetComponent when you only want to get a component the GameObject current script is attached to. To get a component that is attached to another GameObject, you should find that GameObject first then get the component from it. Happy coding!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.