Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 largow · Apr 13, 2014 at 10:00 PM · 2dscenerespawn

Re-setting scenario after respawn

Hello guys, I am making a 2D game, where the main objective is to avoid certain traps that the “environment” has for you. Similar to all the ‘I Wanna be the ...’ series. All the traps consists on different events (OnTriggerEnter2d or OnCollisionEnter2D) that my character activates as he walks in the scenario.

The issue is that in many of them, its position is changed. For example in this code:

 void OnTriggerEnter2D(Collider2D col){

    string name = col.name;
    if ( name == "Trap1Detector") {    
         GameObject trap = GameObject.Find("Trap1") as GameObject;
         float xAct = trap.transform.position.x;
         float yAct = trap.transform.position.y;
         trap.transform.position = new Vector2(xAct,yAct +3);
         col.isTrigger = false;    
    }
 }

And if my player trigger one of those events, and then dies, when he respawn the scenario remains changed, with all the traps already "activated".

Is there a way of "re-setting" or returning the scenario to its dfault state (the one that has one the game starts) when my player dies?

I don't want to do move every single trap to its default state by hand...

The respawning script contains the following code:

 using UnityEngine;
 using System.Collections;
 
 public class Respawn : MonoBehaviour {
 
     public GameObject personaje;
     public Transform ptoDeSpawn;
 
     void OnTriggerEnter2D(Collider2D coll){
         print ("holi");
         Destroy (coll.gameObject);
         if (personaje) {
             GameObject p = Instantiate (personaje, ptoDeSpawn.position, Quaternion.identity) as GameObject;
             CamaraController cam = Camera.main.gameObject.GetComponent<CamaraController> ();
             cam.objetivo = p.transform;
         }
     }
 }


THANKS!

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
Best Answer

Answer by joshpsawyer · Apr 13, 2014 at 10:10 PM

Initially, I was going to suggest just reloading the level, but that doesn't deal with your spawn point. As far as I know, there's no built in way to reset a GameObject, you'll have to do it manually.

On each traps Start() function, you could store it's original state (e.g. transform.position) and then you could implement a Reset() function that simply restores those saved values. You could then call Reset() from your Respawn script?

Alternatively, you could store the ptoDeSpawn (and a flag isRespawning or something) in a PlayerPrefs and reload the entire level, using the stored data as your character's initial spawn point.

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 largow · Apr 17, 2014 at 03:00 PM 0
Share

Thanks, after some research I could implement what I wanted easily using PlayerPrefs. What I do is saving as floats the transform.position of 'ptoDeSpawn', and then when I do Application.LoadLevel(Application.loadedLevel), the spawn point is set to my new spot :)

avatar image
0

Answer by Simon-Larsen · Apr 13, 2014 at 10:14 PM

If you want to reset everything to the way your scene was originally setup, then you can just reload the scene

 Application.LoadLevel(Application.loadedLevel);
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

23 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

Related Questions

Respawning player not working 2 Answers

Changing Scenes on 2D Collision 4 Answers

How to create a save & load system with: System.Serializable ??? 1 Answer

How to change scenes without reloading them. 2 Answers

Assets/Scripts/PlayerController.cs(32,49): error CS0126: An object of a type convertible to `float' is required for the return statement 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