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
1
Question by Reverend-Speed · Aug 28, 2016 at 06:26 PM · getcomponentloadingreferencecachegameobject.find

Why does gameobject.Find/GetComponent not work when a new scene is loaded?

I'm transitioning from from LandingPage scene to ItemFocusTest scene with a singleton script that uses Scenemanager.Loadscene called from a button.

When Unity loads up the ItemFocusTest scene, certain scripts/gameobjects seem to fail to get references to other objects and components in the scene, preventing the scene from working properly.

It's important to note that ItemFocusTest works correctly when loaded manually in the Unity Editor - meshRenderers are found, their material.colors set to color.clear and a reference is cached to the TileClicker script on the Main Camera. (Look for all-caps below)

 void Start () {
         tapTiles                            = tileParent.GetComponentsInChildren<TapTile>();            // Get an array of all the tiles
         oneOfTheTileRenderers               = tapTiles[0].GetComponent<MeshRenderer>();                 // Get the meshrenderer of one of the tiles
         tileMat                             = oneOfTheTileRenderers.sharedMaterial;                     // Find the shared material of all the tiles - so when we fade the material, it fades on all the tiles
         tileMat.color                       = opague;                                                   // Make sure the material is fully visible when the scene loads
         touchIcon                           = GameObject.Find("TouchIcon");
 
         imageFaderMat                       = GameObject.Find("ImageFader").GetComponent<MeshRenderer>().sharedMaterial; // **THIS REFERENCE ISN'T BEING CACHED!**
         imageFaderMat.color                 = Color.clear;
 
         tileClicker                         = GameObject.Find("Main Camera").GetComponent<TileClicker>(); // **NEITHER IS THIS ONE!**
     }

On loading ItemFocusTest from LandingPage, I get the following error message:

MissingComponentException: There is no 'MeshRenderer' attached to the "ImageFader" game object, but a script is trying to access it. You probably need to add a MeshRenderer to the game object "ImageFader". Or your script needs to check if the component is attached before using it. TileCounter.Start () (at Assets/Scripts/Props/TileCounter.cs:41)

At a certain point in itemFocusTest, using enough items SHOULD trigger a set of fades and turn off clicking interaction. Instead, I get the following error...

NullReferenceException: Object reference not set to an instance of an object TileCounter.AddOneToCount () (at Assets/Scripts/Props/TileCounter.cs:73) TapTile.TapInteraction () (at Assets/Scripts/Props/TapTile.cs:44) TileClicker.TapOnTile (RaycastHit target) (at Assets/Scripts/Camera/TileClicker.cs:99) TileClicker.TileIntMouse () (at Assets/Scripts/Camera/TileClicker.cs:76) TileClicker.TileInteraction () (at Assets/Scripts/Camera/TileClicker.cs:41) TileClicker.Update () (at Assets/Scripts/Camera/TileClicker.cs:28)

Again, all this works when the scene is loaded in isolation in the Unity Editor.

What's going on? What's stopping the script from finding the correct gameObjects and/or components? Is there a later event than Start that I can use for initialization? I'm baffled.

--Rev

PS: In passing, there's a question I asked earlier that appears to be held up for moderation - I've no idea why. If there are any mods around, could you take a look at this? http://answers.unity3d.com/questions/1235843/why-will-my-webgl-project-with-streaming-video-not.html

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Reverend-Speed · Sep 02, 2016 at 10:05 PM

This problem remains unsolved, but a workaround is to make the meshrenderer variable public and just assign it in the Inspector. Technically this is worse than doing everything through scripting (I think!), but you can't argue with something that just works.

--Rev

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

53 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

Related Questions

Performance issues with GetComponent/gameObject.Find in different functions 1 Answer

Errors with Script C# [GetComponent, FindGameObject] 1 Answer

.getComponent() creates an Instance? 1 Answer

Assets won't load 0 Answers

Loading an element from an array without loading the whole array? 1 Answer


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