- Home /
Missing Component Exception. Can't find a component that exists.
I have a car in my Resources folder that get loaded like so.
Instantiate(Resources.Load("Car", typeof(GameObject))) as GameObject;
And in that Car is a left and right taillight gameObject with just a light attached.. In a script attached to the car I call this line:
left_light = GameObject.Find("Left_Taillight").GetComponent<Light>();
in Awake() and this in Update()
left_light.enabled = true;
I get the error
MissingComponentException: There is no 'Light' attached to the "Left_Taillight" game object, but a script is trying to access it.
You probably need to add a Light to the game object "Left_Taillight". Or your script needs to check if the component is attached before using it.
So I imagine it is the "Or your script needs to check if the component is attached before using it." because I know there is a Light attached to that object. How can I check if the light exists first? Does my car get loaded into the game bit by bit so it can actually have scripts running before the Light component has been attached to the gameobject (And that's where the error comes from)?
Thanks
The rule of thumb I always follow:
Awake
is used to initialize the inner properties of the current component, and in the Start
, I retrieve the other components and call functions on them.
However, I believe the GetComponent
should work in your case.
Try to do this just to make sure all the components are attached:
Components[] components = GameObject.Find("Left_Taillight").GetComponents<Component>();
for( int i = 0 ; i < component.Length ; ++i )
Debug.Log( component.GetType().ToString(), component ) ;
I have a righttailight that works exactly the same right below it so it seems that Lefttaillight is 'built' a little slower than the right and thus throws the error. I'll try a few suggestions here and see what happens. This is happening a lot so I am hoping to find the best way to check things exist on my car before trying to use them. Thanks
From the scripting reference about GameObject.Find()
: "Finds a GameObject by name and returns it." Pay attention to the indefinite article "a". Could it be that there is another game object in the scene with the same name, but it doesn't have a Light
component on it?
Because of the above uncertainty (also, you can cause bugs with typos), I would recommend not using GameObject.Find()
at all. Ins$$anonymous$$d, I would create a script called CarLogic
or CarReferences
and have public Light leftTailLight;
in it, then attach this script to the resource, and drag the light onto the correct field. Now you only need to call CarReferences carReferences = GetComponent<CarReferences>();
in Awake()
, which is a lot cheaper than GameObject.Find()
.
(Note, it would be even better to make the leftTailLight
a [SerializeField] private
and then provide a public
get property for it, but I didn't want to overcomplicate the explanation).
I don't like using that method either but as a beginner I find it easy to use. I will try and making it more dynamic with your suggestion and see if it fixes anything. Thanks
Answer by TheuerkaufLaurenz · Aug 02, 2018 at 02:00 PM
Find doesn't seem to look for child GameObject's. Search for for the leftlight within your hirarchy by supplying it, or search for the GameObject in a different way.
left_light = GameObject.Find("Car/Left_Taillight").GetComponent<Light>();
Also be sure that the Car actually is named "Car" in the scene. instantiating will usually name it "Car (Clone)" by default
So I have - Car (With Car_Controller.cs attached) -LeftTaillight -RightTaillight
What is the best way to access them? I would prefer not to limit my code with such specific na$$anonymous$$g. Thanks
There would be multiple ways to get your Car object, either defined in your class, or gove it a Tag and then search for the Tag ins$$anonymous$$d of the name. If you have your car you can also use Transform.Find ins$$anonymous$$d of GameObject.find, like myCar.transform.Find. This will search within all child objects of you car
Answer by zakkaiokenx10 · Oct 10, 2020 at 07:01 PM
Personally, I would actually check through the cars children and add them to a list. if the left light needs to change based on a button, you could have a shared bool value for logic and showing. Realistically, there's no moment which that car needs to be setup in the same script. you can tell the car to make the connections when created. Maybe I'm just looking too far into it. I'm sorry.
Your answer
Follow this Question
Related Questions
AppDomain.UnhandledException on Android 0 Answers
Unity seems to be silently suppressing exception dialogs, and showing no messages. Why is that? 1 Answer
Random Unrelated Exceptions thrown in WebGL Build (unity 2018) 1 Answer
ArgumentOutOfRangeException: Argument is out of range? What wrong? 1 Answer
I'm having issue with following stack trace. I can't understand what's causing that. 0 Answers