Hello, so Im learning Unity, and I tried writing flashlight pickup script - everything works great! Except I thought of adding a feature that doesnt let you to pick up multiple flashlight.
So here is the code I need help with:
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 Im disabling gameObject.collider and etc instead of destroying it, because Ill 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?
(That one does not really matter, Ive just thought I should ask)
How to read variable from another script?
I know, I know, ive helluva loads of info for it, but I cant still do it.
So could u please be so kind and help me
Okay so as you can see Im using variable flight in my code above (script is called Trigger.js) Im 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
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.
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.