- Home /
Other
Getting Missing Component Error when in play mode
Hi, I'm Luke, and I'm working on a script for Car lights for my new driving game I'm making, but When I press the play button, I keep getting the same error saying Missing component exception on The headlights, brake lights, even the reverse lights, I tried using Null method but that didn't work. I even tried using the GetComponent Method and that still gave me errors, what Do I do?
here's my code.
void Start () {
light = base.gameObject.GetComponent<Light>();
animation = base.gameObject.GetComponent<Animation>();
if (animation.gameObject.GetComponent<Animation>() != null){
Debug.Log("Can't find animation");
}
}
// Update is called once per frame
void Update () {
if (isOn)
{
light.enabled = true;
}
else
{
light.enabled = false;
}
if (isOn == true && lightType == LightTypes.Indicator)
{
animation.gameObject.GetComponent<Animation>().Play();
}
else
{
animation.gameObject.GetComponent<Animation>().Stop();
}
}
}
Answer by sacredgeometry · Dec 28, 2020 at 06:45 PM
Either your GameObject doesnt have a Light or an Animation component or your animation object doesnt have an Animation Component
well your close,
I'm talking about blinkers here, that's why I posted this
It doesn't matter about your blinkers.
A missing component error is thrown when you try to access a component and unity cant find it on the GameObject.
Unless there is more code the places you have called GetComponent are the likely culprits.
The error you get should tell you exactly which line it is and the stack trace will tell you too.
I've got rid of the GetComponent methods from both lines, what do I do now?
Follow this Question
Related Questions
Animation of Instantiated objects trouble 0 Answers
change the wrap mode of a 2d animation 0 Answers
Fire Animation Help 3 Answers
Can the animation editor create local rotational data? 3 Answers
Adding animation clips via script 2 Answers