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 MedicOnDuty · Dec 04, 2014 at 07:43 AM · sceneenemydatadead

Preserve data within a scene

I'm making a game (specifically a top-down shooter) where there is a main area and several sub-areas saved as separate scenes. This has lead to a problem: if I kill an enemy in the main area, going into a side area, and then come back into the main area, the enemy will still be alive as if I'd never killed him. This is obviously problematic.

I've previously done this with static variables, but the problem here is that all the enemies are prefabs and use the same scripts, and I haven't successfully implemented a manifest-esque script (like the one suggested here). So, in brief, is there a way to keep track of enemy information like position and whether they're alive or dead so that when the player leaves and comes back to a scene, the enemies he killed stay dead?

Here's the code I have so far of the script to remember enemy positions, just in case there's something incredibly basic I'm missing.

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class EnemyStorage {
     public Vector3 lastPos;
     public bool dead;
 
     public EnemyStorage(Vector3 pos, bool d) {
         dead = d;
         lastPos = pos;
     }
 }
 
 public class EnemyPositionsCompound : MonoBehaviour {
 
     public static List<EnemyStorage> enemySaves = new List<EnemyStorage>();
     public static GameObject[] enemies;
     private bool correctLevel;
     private static bool hasBuiltList; //check to see if the list has already been built
 
     // Use this for initialization
     void Start () {
         Debug.Log("checking enemies "+" has build list? "+hasBuiltList);
         //check to see if we're on the right level
         if (Application.loadedLevelName == "Compound") {
             correctLevel = true;
         }
         else {
             correctLevel = false;
         }
 
         //if we are on the right level, run the shit
         if (correctLevel) {
             //get all enemies in the scene
             enemies = GameObject.FindGameObjectsWithTag("Enemy");
 
             if (!hasBuiltList) {
                 foreach (GameObject e in enemies) {
                     Debug.Log(e.name);
                     enemySaves.Add(new EnemyStorage(e.transform.position, e.GetComponent<EnemyHealth>().dead));
                 }
                 hasBuiltList = true;
             }
             
             foreach (EnemyStorage s in enemySaves) {
                 Debug.Log("Enemy status: "+s.lastPos+" "+s.dead);
                 foreach (GameObject e in enemies) {
                     if (s.dead == true) {
                         e.GetComponent<EnemyHealth>().dead = true;
                     }
                 }
             }
         }
     }
     
     // Update is called once per frame
     void Update () {
 
     }
 }

Comment
Add comment · Show 1
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 RudyTheDev · Dec 04, 2014 at 12:05 PM 0
Share

You are assu$$anonymous$$g GameObject.FindGameObjectsWithTag("Enemy"); always returns a list in the same order as before.

Your foreach loops won't work. You take an EnemyStorage. Then you go through all enemies and set them all to dead if the save is dead. So a single dead save will set all enemies to dead. There needs to be a way to tell which enemy corresponds to which save, like a unique ID.

1 Reply

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

Answer by Baste · Dec 04, 2014 at 01:36 PM

PlayerPrefs is your friend! It allows you to store quite a lot of data to disk, without slowing down your application in any way. You can use the current level and some kind of enemy ID to give the enemies unique identifiers, and then you simply save them being dead on death.

So, if you put this on your enemies:

 //Needs to be unique for every enemy on the same scene.
 public int myId;
 string playerPrefsString;

 void Awake() {
     playerPrefsString = Application.loadedLevelName + "/" + myId;
     if (PlayerPrefs.GetInt(playerPrefsString) != 0)
         Destroy(gameObject);
 }

 void Die() {
     PlayerPrefs.SetInt(playerPrefsString, 1);
     //Do the other death stuff!
 }

That'll make sure that they stay dead when returning to the scene. You can use SetFloat and GetFloat in the same way to save and retrieve the enemy's coordinates when returning - I'll let you figure that out yourself.

Oh, and if this is the only thing you're using playerprefs for, you can reset the saved states when restarting the game with PlayerPrefs.DeleteAll(). Otherwise you'll have to write your own reset function.

Hope that helps!

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 MedicOnDuty · Dec 04, 2014 at 03:53 PM 0
Share

Works exactly as I need it to. Thank you so much!

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

How to get the position of a new Instantiated prefab 1 Answer

Share Occlusion Culling Data among Same Scenes 1 Answer

Player Dies when Collide with enemy 2 Answers

enemy targeting player in different scene 1 Answer

How to tell which enemys to create in new battle scene 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