- Home /
Reading variable from other script
I'm new with Unity and I've a problem with reading the variable: 'Selected' from another script . I've read some documentation and that's not working. Can someone help me? Thanks
125*TargetingScript.js*
private var Selected = false;
function Update ()
{
if(Input.GetKey("r"))
{
Selected= false;
}
}
function OnMouseUp()
{
if(Selected == false)
{
Selected= true;
}
}
----------
**TargetingLight.js**
> var newSelected = false;
function Update ()
{
var TargetingScript : Object;
TargetingScript = GetComponent("TargetingScript");
if(TargetingScript.Selected == true)
{
newSelected = true;
}
}
I'm sorry, I can't place the script's in a quote for some reason.
I've formatted your code for you - here's a couple of tips:
Use the code editor to indent by one tab or 4 spaces
Highlight the code and then click the code button
Insert 4 spaces before each line
Answer by Wolfram · Jun 01, 2012 at 06:59 PM
You explicitly declared "Selected" as private, meaning it is inaccessible from outside the script. Just remove "private".
Yep, that too :) Deleted my answer in favour of yours - but I'm guessing a hard cast to Object will lose that reference or do you think not...
I don't really use UnityScript or JavaScript, but I believe your answer was also correct and another problem in his script ;-)