Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by SamohtVII · Aug 02, 2018 at 12:56 PM · exceptions

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

Comment
Add comment · Show 4
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Hellium · Aug 02, 2018 at 01:02 PM 0
Share

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 ) ; 
avatar image SamohtVII Hellium · Aug 03, 2018 at 12:30 AM 0
Share

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

avatar image Harinezumi · Aug 02, 2018 at 02:07 PM 1
Share

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).

avatar image SamohtVII Harinezumi · Aug 03, 2018 at 12:31 AM 1
Share

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image SamohtVII · Aug 03, 2018 at 12:34 AM 0
Share

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

avatar image TheuerkaufLaurenz · Aug 03, 2018 at 06:18 AM 0
Share

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

avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

89 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges