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 Vinytt · Jan 17, 2019 at 01:20 PM · script.scenereferencing

How to attach game objects to scripts via scripting?

So, I'm developing a game in which there's time travel, and the player is allowed to move between two scenes (present and past). The problem is, although I can bring objects from one scene to other using "DontDestroyOnLoad", their references in Scripts aren't brought.

Example: if one of the scripts there's a reference to the player "public gameObject Player;" this reference becomes "missing" in the next scene, as the object "Player" is destroyed in the change of scenes.

How can I attach the new Player of this new loaded scene to the script without adding a "DontDestroyOnLoad" to the player as well?

PS: I've tried to call Player = GameObject.Find("Player"); inside Update, with no sucess

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

3 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by sh_code · Jan 17, 2019 at 02:55 PM

your approach is wrong from the start and will only cause you issues and headaches, all along the way, possibly to a point where you're going to code yourself into a blind alley from which there will be no way out except scrapping most of your project and starting over, with a different approach. very likely the one I am going to suggest now:

you shouldn't have the past and the present as two separate scenes. just have them grouped under two root objects on a single scene, and toggle those objects on and off as the player is moving back and forth in time.

meaning in your scene, you'll have an object "Present", which will be just an empty transform, and everything that exists only in present will be its child. Then you'll have an object "Past", with everything that only exists in the past being child of that. And a third one, something like "Both", where everything that's the same for both present and past will be (so, player, camera, etc). Then, start the game with Past being disabled, and Present active. When player travels, disable Present, and enable Past.

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 Vinytt · Jan 18, 2019 at 01:42 AM 1
Share

Couldn't agree more, I was trying to do it the most difficult way. Your approach is much simplier, I feel ashamed to not have thought of this myself hahaha Thanks ^^

avatar image sh_code Vinytt · Jan 18, 2019 at 01:57 AM 1
Share

we all sometimes get fixated on a specific approach and it makes us overlook any other.

the key is: if you get stuck on an issue like this, where you even find something that looks like a partial solution (DontDestroyOnLoad), but after using it, there's some other weird unexpected problem that seems inappropriately hard to solve (which usually signals the partial solution wasn't supposed to be used for what you're trying to use it), just stop for a while, and (in your $$anonymous$$d, not actually!) throw away everything you've designed and written, and try again from zero, or at least from before the design decision which sent you on that complicated path (in this case it was the separation into two scenes), and consciously think of anything and everything that would help you avoid that.

(not trying to be an unnecessary smartass now, just giving a more general thinking technique that helps, at least to me personally, spot and avoid these blind alley paths ;) ) oh, and glad i helped :)

avatar image
1

Answer by xxmariofer · Jan 17, 2019 at 01:40 PM

If you have a reference to an object in one script, and that object gets destroyed at the change of the scene you will lose it, even if there is that same player object in the other scene.

if in the new scene exist the GameObject called Player you could access it like you tested, but dont do it in the Update since that being called all the time.

You should have a OnLoad method in which you could set up all your vars from that need to reference the new object.

You can subscribe to SceneManager.sceneLoaded if you need any method to be called when loaded.

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
avatar image
0

Answer by badadam · Jan 17, 2019 at 03:03 PM

Player = GameObject.Find("Player") this code finds the object which is just in current scene. Use the script below for endless life

 public class Player : MonoBehaviour {
      public static Player instance;
      private void Awake()
      {
           instance = this;
           DontDestroyOnLoad(this);
      }
      //your other code
 }

You can call it from another scene by using the code below

 Player.instance;


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

125 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

Related Questions

GameObject references runtime script in scene file. Unsure whats wrong 2 Answers

How would I search my scene for a tag 1 Answer

need some help with this things 0 Answers

Saving Player Position between scenes? 1 Answer

To pass a variable between scenes should I use scriptableobject or static ? 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