- Home /
Enable/Disable Scripts
Hey guys. I know this question has been asked a billion times, but I can't seem to find the info I need. I'm trying to make a script where when the Player clicks a flashlight, the flashlight activates the flashlight script attached to the player while the model that the Player clicks destroys itself. While the model is destroyed, the script doesn't activate. Any help?
var Flashlight : Flashlight;
function Start() {
Flashlight = GetComponent<Flashlight>();
}
function OnMouseDown() {
Flashlight.enabled = true;
Destroy(gameObject);
}
Answer by JSierraAKAMC · May 31, 2015 at 12:57 AM
var flashlight : Flashlight;
function Start() {
//GetComponent<Type>() is used in C#, while GetComponent(Type) is used in JS
flashlight = GameObject.FindWithTag("Player").GetComponent(Flashlight);
}
function OnMouseDown() {
flashlight.enabled = true;
//Also, this line will cause troubles
Destroy(gameObject);
}
I found two errors in the code. The first is the GetComponent()
call. It was an easy fix. The second problem that you would run into is that your Flashlight component would end up being destroyed along with the GameObject it's attached to. flashlight
is a component on the GameObject (according to the code) and Destroy(gameObject)
is called, destroying the GameObject and the components. However, you stated that you would like the Flashlight
component on the player to be enabled, correct? If so, you should tag the player as "Player" or something similar and change the line flashlight = GetComponent(Flashlight);
to flashlight = GameObject.FindWithTag("Player").GetComponent(Flashlight);
Hope it works!
It's not giving me the errors, however, it won't let me click the flashlight gameobject and destroy it.
Never$$anonymous$$d I tinkered with some things and fixed it. Thank you so much for your help!
Answer by awplays49 · May 30, 2015 at 09:16 PM
Hi @GraviterX,
Try changing the Flashlight variable representative to a lower case "f". Unity is probably getting your variable confused with the component. Is the word Flashlight (variable) a different color? If so, then problem solved :)
var flashlight : Flashlight
EDIT
Also, always use lower case letters for first words of custom variables. If you don't, at some point it could be the same name as a script or something already taken, then you'll have to change everything.
Also, it would be great if you would accept this answer if it helped :)
It didn't change anything, but thanks for the advice I forgot to mention that I'm getting these errors (sorry, I forgot to check the console for probably the most important reason why it was failing) -(4,47): BCE0043: Unexpected token: ). -(4,48): BCE0044: expecting ), found ';'. -(4,49): UCE0001: ';' expected. Insert a semicolon at the end.
This doesn't happen, the compiler is smart enough to know when to use the type and when to use the instance. If the method or member is static then it will asume you wanted to use the type, and if it's not static it'll asume you're using the instance.
The only time it doesn't work is if your variable is called like a class, but it's not a variable of that class and you'll get a compiler error if (and only if) the variable type doesn't have the method or member you're trying to access.
If you mix them (trying to access a static method or variable with an instance or trying to access non-static with the type) you'll get a compiler error.
The last part about starting variables always with lowercase it's hardly an "always do this" thing, it isn't even neccesary. If for some reason you end up with a variable in a script called like a new class you created, the code will still work just like before.
Answer by DiegoSLTS · May 31, 2015 at 01:30 AM
This line:
Flashlight = GetComponent<Flashlight>();
Should look like this:
Flashlight = GetComponent.<Flashlight>();
if you want to use the generic version of GetComponent. Note the dot after GetComponent. With this you won't get those compiler errors.