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 SesshogeGames · Nov 21, 2018 at 09:32 PM · 2d gamedontdestroyonloadcamera followfollow playerscene change

Problem using DontDestroyOnLoad carrying over a player, but not a camera

So, I am trying to make an overhead 2D RPG. The tutorial series I am watching carried over both the Camera and the Player, but I want the camera background to have a different color in this different scene. Now here's my question, is there a way to have the camera follow something that is in the DontDestroyOnLoad category? Or is there some way to have this Camera change background color when it switches to a new scene?

 DontDestroyOnLoad(transform.gameObject);
         if (GameObject.Find(gameObject.name)
                  && GameObject.Find(gameObject.name) != this.gameObject)
         {
             Destroy(GameObject.Find(gameObject.name));
         }

(This is the code I use for the Player)

 {
     public GameObject followTarget;
     private Vector3 targetPos;
     public float moveSpeed;
     
         // Use this for initialization
         void Start () {
     
         }
         
         // Update is called once per frame
         void Update () {
             targetPos = new Vector3(followTarget.transform.position.x, followTarget.transform.position.y, transform.position.z);
             transform.position = Vector3.Lerp(transform.position, targetPos, moveSpeed * Time.deltaTime);
         }
 }

(And this is the camera script)

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

2 Replies

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

Answer by Happeloy · Nov 21, 2018 at 09:44 PM

I'm not totally sure that this is what you mean, but a great way to always be able to get a component, as long as it is alive, no matter if it was instantiated in the same scene or not, is to add a script on it that has a static reference that you can call from anywhere.

Lets say you want to get the position of an object that was instantiated in another scene with DontDestroyOnLoad. If you put a script on that object that looks like this:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class ObjectThatIsAlive : MonoBehaviour {
 
     public static ObjectThatIsAlive Instance { get; private set; }

     private void Awake()
     {
         DontDestroyOnLoad(gameObject);
         Instance = this;
     }
     public Vector3 GetCurrentPosition(){
         return transform.position;
     }
 }

You can then access this from any other script at any time by calling

     Vector3 position = ObjectThatIsAlive.Instance.GetCurrentPosition();



Note that if you wanna do this the right way, you should look at how a singleton is implemented. The problem here is that if you do this, you can't use this script on any other gameobject, since there can never be more than one static reference. If you add this script to another component, the latest invoked one will be the one with the static reference. If you want to destroy this object at any time, you'll have to set the static reference to null and then have a check for that when ever you want to use it.

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 SesshogeGames · Nov 22, 2018 at 04:22 PM 0
Share

I'll try it out, thanks!

avatar image
1

Answer by ecv80 · Nov 21, 2018 at 10:18 PM

You can do any of the two things.

If you want to carry over both your player and camera, you can change your camera settings (like your background) right after your new scene load call, as the script from where you're loading the new scene will still run to completion. It won't return immediately after you load the new scene. Or another way is by having an object (for example an empty) in your new scene, find your camera in its Awake() and change its settings there.

If you want to carry your player only with DontDestroyOnLoad, you can find your player from your new camera Awake() in your new scene and set it as the target.

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 SesshogeGames · Nov 22, 2018 at 04:46 PM 0
Share

How exactly do you code a change in the camera background color?

avatar image ecv80 SesshogeGames · Nov 22, 2018 at 04:57 PM 1
Share

For example: myCam.GetComponent<Camera>().backgroundColor=Color.black; right after your Scene$$anonymous$$anager.LoadScene("sceneName"); . Or somewhere in your Awake() in some game object in your new scene after you find the camera. Also you may use Camera.main to get it if you only have one camera.

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

107 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

Related Questions

Why sprites are getting blur when camera is in motion? 0 Answers

camera script not working 1 Answer

NPC Party Member Direction 1 Answer

Keeping variables between scenes with scriptable objects? 0 Answers

camera problem 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