- Home /
Light enabled property copied on Instantiate
I'm seeing a weird behavior when trying to clone an object. Basically I have a simple cube with a light attached to it. The light is disabled by default. What I wanted to do was, on collision for the box, enable the light and create a new cube to repeat the process. I built the cube and light into a prefab and attached my script which looks like this:
var lightBox : Transform;
private var duped = false;
function OnCollisionEnter(collision : Collision) {
if (!duped) {
var newInstance = Instantiate(lightBox, Vector3 (0, 10, 0), Quaternion.identity);
newInstance.light.color = Color(Random.Range(0.0, 1.0), Random.Range(0.0, 1.0), Random.Range(0.0, 1.0), 1);
duped = true;
}
light.enabled = true;
}
What I've found is that if I enable my light before Instantiating the new box (i.e. moving the 'light.enabled = true' to be the first line in the method), the new box's light is enabled immediately. If I enable the light afterwards as shown in the code above, the new box's light is disabled until it collides (the intended behavior).
Is this a bug or is there a reason why enabling the light on one object would cause the light on the next one to be enabled immediately?
Answer by Anxo · Jun 13, 2011 at 03:33 PM
hmm not sure but if you just wanted to make sure that the light if off on every instances you could see if this would work
newInstance.light.enabled = false;
Thanks for the reply. I have tried that, but I'd rather figure out the root cause and/or the correct pattern for instantiating prefabs that way the properties I set in the visual designer are applied when instantiated.
Your answer
Follow this Question
Related Questions
Instantiating Child - Prefabs 1 Answer
Access Child of instantiated UI-Prefab using C# in Unity Beta 4.6 1 Answer
Instantiating a prefab through C# script 0 Answers
prefab question 1 Answer