Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 JosGro · Sep 07, 2021 at 10:47 AM · scenefind

GameObject Find isnt working in the same method with SceneManagment

Hello, I have the following problem: Im trying to swap to a scene(„samplescene“) using GameObject(this skript is attached to it) wich uses „DontDestroyOnLoad“ Then I want it to get a refference to a GameObject(„housespawner“) which I will use for the next steps (inside „samplescene“) The loading of the scene works fine, but the .Find method doesnt work in NewGame() which is confusing to me because the same command works if im manually doing it with Update()/GetKeyDown after I loaded the scene (Update() is just there,to show you the way it worked). So wheres the cause of not working when Im using it in the same method as SceneManager.LoadScene and what could be a solution. Or is there a even better way by not using .Find (I know its not very perfect). I was also searching for a way to refference housespawner immedialty, but the Gameobject with the skript is in the scene before. Thanks


public class GameManager : MonoBehaviour{

  public void NewGame()
  {
     SceneManager.LoadScene("SampleScene");
     
     houseSpawner = GameObject.Find("houseSpawner");
 }

 void Update()
 {
     if (Input.GetKeyDown(KeyCode.K))
     {
         houseSpawner = GameObject.Find("houseSpawner");
     }
 }

}

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
0

Answer by Bonfire-Boy · Sep 07, 2021 at 11:01 AM

I guess the scene hasn't finished loading when you make that call. Try adding a callback like this...

 private void Awake()
 {
     UnityEngine.SceneManagement.SceneManager.sceneLoaded += HandleSceneLoaded;
 }
 
 
 private void HandleSceneLoaded(UnityEngine.SceneManagement.Scene scene, UnityEngine.SceneManagement.LoadSceneMode mode)
 {
     houseSpawner = GameObject.Find("houseSpawner");
     Debug.LogFormat("housspawner found: {0}", houespawner != null); 
 }
 

As for whether or not there's a better way than using Find... if this is something you just need to do once after the scene has loaded then I wouldn't worry about it too much tbh. But there are several alternatives. These include 1) if appropriate, which it looks like it might be, make the housespawner a singleton, 2) decouple the classes and control the Housespawner class using events (so that the GameManager doesn't need to have a reference to the housespawner at all) and 3) make the housespawner persist as well.

Comment
Add comment · Show 1 · 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 JosGro · Sep 07, 2021 at 11:05 AM 0
Share

Thanks a lot

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

153 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 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

How Can i Find all gameobject in All scene 2 Answers

How can i find out how much element on scene now? 1 Answer

Find an object in a new scene that hasn't been destroyed? 4 Answers

Find how many are there in the scene 1 Answer

Urgent please. Can't Find Scene in build settings while scene is in build settings. 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