- Home /
assign a script to a variable
Hey,
How can I assign a script to a variable?
For example, I have a controls.js, which is attached to a game object. I also have an other script, manager.js
In manager.js how can I do the follwing:
var controlScript : ??;
var object : GameObject;
function Start()
{
controlScript = object.GetComponent("controls");
}
Thanks for your time!
Answer by syclamoth · Jan 23, 2012 at 03:27 PM
Well, in UnityScript, the filename is also the typename!
So, if you have a file called controls.js, you can define a variable of that type with
var controlScript : controls;
It's similar to how you are using GetComponent, later- the typename in quotes that you specified in the string paramater is the same as the one you would use when declaring the variable.
(In C# you have to make sure you set the typename to be the same as the filename manually, because although it compiles properly, Unity gets angry if you try to attach it to an object if it's wrong!)
What if the script you are trying to assign to the variable is a C# script?
did you ever find an answer to that question? I need to know as well.
Have you considered googling it? No worries, I did it for you.
http://www.41post.com/1935/program$$anonymous$$g/unity3d-js-cs-or-cs-js-access
Your answer
Follow this Question
Related Questions
Is there a way to make a variable correspond to multiple different scripts? 1 Answer
Enemy Health Problems 1 Answer
Changing variable from another script 1 Answer
How to modify variables from scripts in standard package? 2 Answers
UnassignedReferenceException: The variable enemy of EnemiesSpawner has not been assigned unity3d 0 Answers