- Home /
Accessing external scripts (yet again)
Yes, I recognize that there are 50 answers to this question, but I've read everything I can find and its just not registering with me.
If someone could just help me with this maybe I can move on.
I started here, but it seems to be riddled with either pseudo-code or its not giving me enough information to actually understand what they're talking about. They use the term "otherScript" too frequently.
So, my specific example, should be very easy. Forgive me if I give too much info, I just wanna be sure we're on the same page.
Here's what I've got:
I created a game object in my scene, its named "_globalScripts"
It has a script attached to it named "animationManager.cs"
animationManager.cs has a class named animationManager : MonoBehavior
I have another script, "behaviorController.js" (javascript) attached to a vehicle
Within behavior controller I want to call animationManager.sayHello(), which does a quick Debug.Log
The reason I have a mixture of C# and JS is because I'm trying to learn to make the transition. My newer stuff is going to be C#.
Please tell me what I should add to behaviorController's members (like, private var), what I should add to Start() to attach the reference, and what I type in Update() to call sayHello().
I know its just my ignorance, but the docs seem to suggest that I can create a private variable of type 'animationManager' (as implied by OtherScript).. but I get unrecognized type errors on that. I've also managed to get a reference to animationController through GameObject.find("_globalScripts").GetComponent("animationManager") .. but without proper typecasting I cannot execute sayHello().
Subquestion: Is it possible to skip adding anything to Start() and just call my reference gathering stuff directly in the JS class members, i.e: private var animationManager : something = GameObject.find(....)...?
Thanks for your help.
Answer by Khada · Aug 20, 2012 at 10:23 AM
Here is an example, you will have to alter it to your needs (I'll use the class/object names from your question):
//----------------------------------------------------------
//animationManager.cs
//----------------------------------------------------------
//make sure this exists in the class and is public
public void sayHello()
{
Debug.Log("hello!");
}
//----------------------------------------------------------
//behaviorController.cs (using cs as i don't use js)
//----------------------------------------------------------
//this will be our reference to the animation manager class
animationManager kAnimMan = null;
void Start()
{
//get the global manager object
GameObject kGloMan = GameObject.Find("_globalScripts");
//get the animation component from it and store it
kAnimMan = kGloMan.GetComponent<animationManager>();
}
void Update()
{
//will tell the animation manager to say hello every frame
kAnimMan.sayHello();
}
Note: I wrote that off the top of my head, I can't promise it's 100% perfect.
EDIT: To answer your sub question, no (though you could use a field).
I appreciate the response. Your explanation falls exactly in line with my understanding. The JS adaptation of what you put there is almost identical. I put "private var kAnim$$anonymous$$an : animation$$anonymous$$anager" in the code and I get an error "The name 'animation$$anonymous$$anager' does not denote a valid type ('not found'). Did you mean UnityEditor.AnimationClip..."
I am doing something wrong in exposing that animation$$anonymous$$anager class, it fails even before I can sayHello().. but both the animation$$anonymous$$anager class and sayHello are public.
Ahh no, it was a typo, animation$$anonymous$$anager in the Update func should be kAnim$$anonymous$$an. I'll edit it.
$$anonymous$$y script fails on the declaration of kAnim$$anonymous$$an as type 'animation$$anonymous$$anager', long before Update() is even called. Unity is not seeing animation$$anonymous$$anager as a valid type. I did not copy+paste your code, I already calling "kAnim$$anonymous$$an.sayHello()", so the typo wasn't the cause of my troubles. Thanks :)
To check: You have a class called animation$$anonymous$$anager right (according to your original question you do)?
Did that do the trick? Don't forget to mark the question as answered if it did.
Answer by fafase · Aug 20, 2012 at 09:02 AM
Have you seen this one? http://answers.unity3d.com/questions/246211/having-scripts-interact.html
It is only UnityScript but the idea goes the same with C#.
Try placing the C# script in the standard asset folder and keep the Js outside.
Scripts inside the std asset are compiled before others.
Thank you for the response. $$anonymous$$y error happens at "var other : ScriptName;", or in my case "var kAnim$$anonymous$$an : animation$$anonymous$$anager;" -- I get the error "The name 'animation$$anonymous$$anager' does not denote a valid type ('not found'). Did you mean..."
Every answer I find is the same, I can't figure out what I'm doing wrong in exposing animation$$anonymous$$anager as a type.
your issue might be the "" around the component name:
GameObject.Find("_globalScripts").GetComponent(animation$$anonymous$$anager);
try and remove. I have seen cases here where the "" would create error or null. And it is Find not find, but that oculd just be a wrong copy paste.
I've tried that as well. I am actually failing on the declaration of the class member, before Start() or Update() ever run.
Your answer
