- Home /
Variable problem in Javascript.
Hello, so I`m learning Unity, and I tried writing flashlight pickup script - everything works great! Except I thought of adding a feature that does`nt let you to pick up multiple flashlight. So here is the code I need help with:
var flight;
flight = 0;
function OnTriggerEnter (other : Collider)
{
if(flight == 0)
{
gameObject.collier.enabled = false;
gameObject.renderer.enabled = false;
gameObject.light.enabled = false;
flight = 1;
}
}
Ok, so the problem is if(flight == 0) does not seem to have any influence when picking second flashlight it just picks both of them no matter what. I know that flight is being set to 1, because I tried adding Debug.Log(flight) to the script and it does set it to 1, but second flashlight still can be picked. I tried setting flight to 1 at the start of the script where flight = 0 is and then it wont pick it up, so I`m really confused. Please help.
Also until I`m here I have few more questions for you people to maybe help me with, ok here it is:
As you can see I`m disabling gameObject.collider and etc instead of destroying it, because I`ll need it to re-enable in certain amount of time, so is there any shorter way to disable everyting something like gameObject.enabled = false. huh? :D (That one does not really matter, Ive just thought I should ask)
How to read variable from another script? I know, I know, i`ve helluva loads of info for it, but I can`t still do it. So could u please be so kind and help me :P
Okay so as you can see I`m using variable flight in my code above (script is called Trigger.js) I`m also using the same variable in my other script (flashlight.js) and I would like to instead of creating new variable in Trigger.js to somehow extract it from flashlight.js. That sounds confusing but I hope u`ll understand me. Sorry 4 my bad English. Peace :P
Answer by MileSplit · Apr 20, 2014 at 07:49 PM
Your problem is that your variable flight is local, meaning that is exists only in that script. When you have another flashlight ITS flight is still 0 even though your original one is 1. A good example would be a health variable. The health only describes the gameobject its attached to, not every single object. You can easily fix this with a static variable. [https://unity3d.com/learn/tutorials/modules/intermediate/scripting/statics][1] A static variable is a variable that exists only once across all scripts.
static var flight = 0;
function OnTriggerEnter (other : Collider)
{
if(flight == 0)
{
gameObject.setEnabled(false);
flight = 1;
}
}
Accessing other scripts is easy also.
GameObject.Find("Name of Object").GetComponent("NameOfScript").variableYouWishToAccess
Good Luck! [1]: https://unity3d.com/learn/tutorials/modules/intermediate/scripting/statics
Thank you so much :) Very nicely explained, and really makes me understand what I`m doing. haha On the gameObject.setEnabled(false); I kept getting and error, so I`ve found another way if anyone else reading this needs it: gameObject.active = false;
Cheers mate :))
Answer by Griffo · Apr 20, 2014 at 07:56 PM
You need to add a empty GameObject to your scene, name it something like flashLighVar, make a .js and call it enableFlashlight.js
Then your script above get a reference to the new GameObject flashLighVar
var flashlight : GameObject;
function Start(){
flashlight = GameObject.Find("flashLighVar");
}
Also in the script add a reference to the ne javascript enableFlashlight.js
var flaslightScript : enableFlashlight;
function Start(){
flaslightScript = flashlight.GetComponent(enableFlashlight);
}
Then in the enableFlashlight.js add a variable
var allowFlashlight : boolean;
Then reference it
if(flashlightScript.allowFlashlight){
// allow flashlight to be picked up
// then set it to false so you can not pick another flashlight up
flashlightScript.allowFlashlight = false;
}
Not tested, but should help point you in the right direction.
Answer by Burla · Apr 20, 2014 at 07:57 PM
You can achieve it this way. First declare this variable at the top of your Trigger.js
flashlight thisFlashlight;
Then use the GetComponent() function inside your Start() function.
void Start() {
thisFlashlight = GetComponent<flashlight>();
}
Now you can access the variable through thisFlashlight like the following.
Type myVariable = thisFlashlight.variableName;
Let me know if this helped you.