• 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 /
  • Help Room /
avatar image
0
Question by mrtyninja · Feb 27, 2016 at 11:49 PM · charactercontrollerdestroycharacter controllerdontdestroyonload

Help with exception to DontDestroyOnLoad

I'm having a bit of a complicated issue so bear with me while I try to explain it.

Each level in my game is divded into 3 seperate scenes. Each scene represents a different stage of "alert". (For example if the player steps in front of a security camera it triggers the "alarm" version of the level).

Im useing a basic DontDestroyOnLoad script attached to the player character in order to keep the players position during transition between these stages.

     function Awake(){
     DontDestroyOnLoad (this);
     }

HOWEVER (this is where things start getting tricky to explain) on starting a new level, The player needs to move to the beginning of the level, which of course, is at a different position then the location where the last level ended.

I've tried inserting a second player character and using the destroy function on an empty game object in the level to destroy the old character but that doesn't seem to work for me. I just end up with two player chacters in the level.

     function Start() {
      
     Destroy(gameObject.Find("character1"));
     }
 

Note: I also posted this question on the forum as I'm not entirely sure where it belongs.

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
0

Answer by Zoogyburger · Feb 28, 2016 at 02:13 AM

This will make sure the player is not destroyed or duplicated in c#:

     if (!playerExists) {
         playerExists = true;
         DontDestroyOnLoad (transform.gameObject);

     } else {
         Destroy (gameObject);
     }
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 mrtyninja · Feb 28, 2016 at 12:14 PM 0
Share

I don't think I did a very good job explaining my problem. The problem isn't that the player is being destroyed. But rather that I need the player to be destroyed on exiting certain scenes but not others.

avatar image
0

Answer by mrtyninja · Feb 28, 2016 at 03:18 PM

I figured out a temporary solution in case anyone else is having a similar issue. Its not the most elegant solution, But what I did was add an empty game object in the area where I want the player to start and created a box collider in the area where the player actually starts due to the DontDestroyOnLoad script. var destination : Transform;

 function OnTriggerEnter(other : Collider) {
     other.transform.position = destination.position;
 }
 

Set the transform position to the empty game object and it works as a temporary fix.

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

57 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

Related Questions

First person movement with character controller does not detect ground properly 0 Answers

Rope & Character controller ? 0 Answers

how do I import the character controller package 1 Answer

How to move a character in one direction only when jumping? 1 Answer

I have a problem in the jump system. 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