- Home /
Typecasting problem?
Hello!
I have GameObject named "EyeLight". I've buried "EyeLight" deep in heiarchy of Game objects. How can I reach it to enable / disable?
Everything I've tried so far has been bunk -
Light o_EyeLight;
Void Start()
{
o_EyeLight = GameObject.Find ("EyeLight");
}
public void EyeLight(bool eyelight)
{
Light myEyeLight = o_EyeLight;
if (myEyeLight != null)
{
if (eyelight) {
myEyeLight.enabled = true;
} else {
myEyeLight.enabled = false;
}
}
}
The code rips at type conversion. I'm not sure what I'm doing wrong.
Thank you all for response! I selected the most complete answer for those who might come here down the road. One thing I want to mention, is that it's important to have on the GameObject named "EyeLight" while keeping the light component off so the script can handle when the light is on and off ins$$anonymous$$d.
If you turn off the game object, you will receive a null object error because the game doesn't create the object instance.
Answer by bryanwaters · Jan 30, 2017 at 02:07 PM
This should work try the code below
GameObject o_EyeLight; //sets to GameObject so it can use the (GameObject.Find)
Void Start()
{
o_EyeLight = GameObject.Find ("EyeLight");
}
public void EyeLight(bool eyelight)
{
Light myEyeLight = o_EyeLight.GetComponent<Light>(); //look for the Light component
if (o_EyeLight != null) // now it's looking if it found an object not the light component
{
if (eyelight) {
myEyeLight.enabled = true;
} else {
myEyeLight.enabled = false;
}
}
}
Thank you all for responding! I chose this answer because it's the most complete, and I want others who might come after me to have a clear path. To note:
One must make sure the Light component of EyeLight Game Object is turned off, and the game object itself is on, otherwise you'll get a null object error.
That's not exactly accurate, and it is always good to perform null check, the Object.Find method will return null if the object is disabled, personally I do not suggest using Object.Find for almost anything. The are almost always much better ways of finding objects, and searching by Name, makes it iterate trough every GameObject until it finds what it is looking for. Its usually not a big deal on start sections or OnEnable, but if you consider this to be irrelevant then it will turn into an issue later on, and you'll have laggy starts on scene loading.
I agree. a better way would be to use [serializefield] for the GameObject EyeLight to gain the ability to drop the GameObject.Find(); altogether. And to refine it more, to drop down the frequency of doing .GetComponent("Light"); why not just add a small script to the eyelight GameObject directly, remove the code from the controller, with use the Get$$anonymous$$ey in the Update on the EyeLight object, and create a way around needing to call GetComponent("Light"); each function call by having it set in the start() of that object.
Thanks for all the wonderful feedback!
Answer by aditya · Jan 30, 2017 at 01:19 PM
Gameobject.Find returns a gameobject not a Light .... try this in your Start method
Light o_EyeLight;
Void Start()
{
// Use GetComponent as least as you can as it increases the chances of GC (As said on a forum pos)
o_EyeLight = GameObject.Find ("EyeLight").GetComponent<Light>();
}
public void EyeLight(bool eyelight)
{
Light myEyeLight = o_EyeLight;
if (myEyeLight != null)
{
if (eyelight) {
myEyeLight.enabled = true;
} else {
myEyeLight.enabled = false;
}
}
}
Answer updated for future readers as OP found it incomplete
Selected answer is correct as well but with two problems
GetComponent is used in EyeLight method which means it will get called as many times as EyeLight will be called
OP defined o_EyeLight as Light but in answer it is defined as GameObject and a new variable is created for Light
I'd favour this solution since it only calls GetComponent() once ins$$anonymous$$d of every time you toggle the light.
I would also shorten this block:
if (eyelight) {
myEyeLight.enabled = true;
} else {
myEyeLight.enabled = false;
}
to:
myEyeLight.enabled = eyelight;
Your answer
Follow this Question
Related Questions
How to enable or disable a script OnTriggerEnter? 1 Answer
Locate a game object using rays 1 Answer
Convert a GameObject to a transform? 1 Answer
How to make a GameObject not hittable by shadows ? 2 Answers
Affect All Instances Of A Prefab 3 Answers