- Home /
Accessing other scripts is so confusing..How?
Hi, To me the most difficult part of unity..really is to make gameobjects and script talk to each other. It should be much easier. Anyway
Q.1) I did exactly what the Unity reference says:
function Update () {
var script:scriptname = GetComponent(scriptname);
if (Input.GetKeyDown("space")){
scriptname.Something();
}
}
I have this script attached to a gameobject. The scriptname.js is attached to another gameobject in scene that by the way it's a prefab. I get the usual (and rather annoying) "Object reference not set to an instance of an object...." What's wrong?
Q.2) I have a whole bunch of objects in scene with the same script attached to them (scriptA). The script has allready a variable of GameObject type. That GameObject has a script (scriptB) to which I want script A to talk to and I don't want to sit down and do this draging and dropping in the inspector. It will take much time.
Thanks in advance for your help.
Answer by kieblera5 · Sep 09, 2011 at 07:15 PM
var myObject : GameObject;
var objectScript : myScript;
function Start()
{
objectScript=myObject.GetComponent(myScript);
objectScript.doStuff();
}
No need for the .transform.
Thanks for helping..Well. I do EXACTLY that and I get this error: No appropriate version of 'UnityEngine.GameObject.GetComponent' for the argument list '(UnityEngine.GameObject)' was found."
The object this script is attached to, is an object that is child of the other object I want to "talk" to by the way. If that matters at all.
In that case.. just say objectScript=parent.GetComponent(myScript);
um..it sayd unknown identifier. I've tried objectScript=transform.parent.GetComponent(myScript); but I get the same error as before.
O.o
I'm stupid today XD in GetComponent, put the myScript in double-quotes. i.e. transform.parent.GetComponent("myScript");
I can't believe that was the problem.. O.o.O.o Thanks everyone for helping me out. Really :-)
Answer by testure · Sep 09, 2011 at 06:39 PM
Well, the first problem i see is that you're not using the variable you created. You're using the actual class name (scriptname).
Assuming that's just pseudocode and not the actual code you're using, you could be running into a problem with scope. You're simply calling GetComponent(scriptname), which will only look for a script with that name on the current game object.. the one that you're running this script from. You need to get a reference to the other game object first, which means you'd need to do something like this:
GameObject.Find("gameobjectName").transform.GetComponent(scriptname);
That's how you'd do it in C# anyway.. the syntax for JS should be very similar if not identical.
As a side note- you almost never want to use GetComponent in Update. It can be a framerate killer if you're not careful.. if you're doing something so often that it gets used in every frame, you will want to cache the reference in Awake or Start first, and then use the reference in Update.
Something like this (just winging javascript here, it may not be 100% accurate):
var script:scriptname;
function Awake() { script = GameObject.Find("gameobjectName").transform.GetComponent(scriptname); }
function Update() { script.DoStuff(); }
Just to add to your answer: The type when you declare the var has to be the same as in the GetComponent. It might be a typo, but at the top you have 'scriptname' and in GetComponent you have 'scriptName'
ah yeah, good call. i've edited it. the dangers of coding off the top of your head into a web browser :)
Thanks for you help. made me understand a thing or two. But I don't understand what's wrong with the following:
var object:GameObject; //I've set this in inspector
var objectscript:script;
function Start(){
objectscript = object.transform.GetComponent(script); objectscript.DoStuff();
}
nothing jumps out at me as being wrong with that code (other than some forum line-break issues, which are obviously not in your actual code).
Are you getting an error, or is it doing something unexpected?
Your answer
Follow this Question
Related Questions
Best way to Reference Child from Parent's script? 3 Answers
Referencing a Public GameObject from another script. 2 Answers
Cannot use System.data as reference 0 Answers
Variable not changing when you get a reference from another script 1 Answer
How do I store a public reference to a GameObject/GameObject's Component? 1 Answer