The question is answered, right answer was accepted
Why is my GameObject that is attached to a script disappearing after I Play the scene.
So, my Light component keeps disappearing right after I play the scene. Does anyone know what seems to be the problem?
My console is also giving me this message: "MissingComponentException: There is no 'Light' attached to the "Kansi" game object, but a script is trying to access it. You probably need to add a Light to the game object "Kansi". Or your script needs to check if the component is attached before using it. LidOpen.Update () (at Assets/Scripts/LidOpen.cs:22)"
A component or game object can only disappear from the scene (without user interaction) if a script calls Destroy()
on it. So there must be a script that takes your Light game object or the Light component on it and destroys it.
Possibly it is the LidOpen script itself that does it, but it can be anywhere in your scene. After checking LidOpen, I recommend a text search for Destroy(light)
or Destroy(
(without closing parenthesis) in all your scripts.
Answer by upasnavig90 · Mar 02, 2018 at 10:12 AM
hi, what happened was, @tuomvii was getting light component which was not attached to the same gameobject that of script. so he has to remove the line :
Light = GetComponent();
and he was to give the reference from outside, or use gameObject.find() function.
Answer by NEGATIVERAGDOLL · Mar 02, 2018 at 09:14 AM
Could you show the script it is referring too? You probably destroy the object by accident
Sure,
public Animator anim;
public bool lidCheck;
public Light Light;
void Start()
{
anim = GetComponent<Animator>();
lidCheck = false;
Light = GetComponent<Light>();
}
void Update()
{
if (lidCheck == false)
{
Light.range = 2.0f;
}
}
void OnTriggerStay (Collider other)
{
if (other.gameObject.tag == "Player")
{
if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.E))
{
anim.Play("openlid");
lidCheck = true;
}
if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.R))
{
anim.Play("closelid");
lidCheck = false;
}
}
}
}
is the light component attached to the same gameObject to which script is attached??
No it is not. The script is attached to the object which is animated. The light component is in another object.
but you are fetching light component from the same object as in this line: GetComponent();
either try gameObject.find or assign it from outside as public varible. if you are already assigning from outside just remove this line: Light = GetComponent();
What I did is that I put the script to the object which i wanted to be animated. Then I put the other object which has the light component as an attachment to the public Light slot.
Guys, I took the liberty and converted the discussion into comments. I hope you don't $$anonymous$$d. Never $$anonymous$$d, it was upasnavig90 who helped...
Follow this Question
Related Questions
How can you delete a MeshCollider?? CAN you delete a MeshCollider? 1 Answer
Storing any component inside a single variable. 1 Answer
How to write type-safe loosely coupled components 1 Answer
How to add a editing option to variables in a script in Inspector? 0 Answers
I'm receiving an error saying that "Specified cast is not valid (wrapper castclass)". 0 Answers