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 Kek_Kek · Dec 14, 2018 at 01:29 AM · intinteger

Script not changing int variable

I am trying to make it so when an enemy touches you the player, and an integer on another script equals 0, the integer switches to a 1. Problem is that the int is not changing.

The Int script:

 [System.Serializable]

 public class SaveFile  {
 
     public int Respawn;
         
 }

The enemy script:

 public int Scene;
 public Transform target; 
 NavMeshAgent agent;

 public SaveFile respawn;
 // Use this for initialization
 void Start () {
     agent = GetComponent<NavMeshAgent>();
 }
 
 // Update is called once per frame
 void Update () {
     agent.SetDestination(target.position);
     
 }
 public void OnTriggerEnter(Collider other)
 {
     if(other.tag == "Player")
     {
         if(respawn.Respawn == 0)
         {
             respawn.Respawn = 1;
             SceneManager.LoadScene(Scene);
            
         }
         if (respawn.Respawn == 1)
         {
             SceneManager.LoadScene(Scene);
             respawn.Respawn = 2;
         }
         if (respawn.Respawn == 2)
         {
             SceneManager.LoadScene(Scene);
             respawn.Respawn = 3;
         }
     }
 }




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 Sabre-Runner · Dec 14, 2018 at 04:16 AM

When you load the scene, any previous runtime data is destroyed. Even monobehaviours on gameobjects are destroyed so there will be nothing to save on.

If you want to save data between scene reloads you should use an object with DontDestroyOnLoad, a ScriptableObject, PlayerPrefs, or save it to file somewhere.

Comment
Add comment · Show 4 · 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 Kek_Kek · Dec 15, 2018 at 12:08 AM 0
Share

I added this to the script the script and have it initialized in void Start however it still doesn't work.

   public void SaveSettings()
     {
         string jsonData = JsonUtility.ToJson(respawn, true);
         File.WriteAllText(Application.persistentDataPath + "/SaveFile.json", jsonData);
     }
 
     public void LoadSettings()
     {
 
         if (File.Exists(Application.persistentDataPath + "/SaveFile.json") == true)
         {
             File.ReadAllText(Application.persistentDataPath + "/SaveFile.json");
             respawn = JsonUtility.FromJson<SaveFile>(File.ReadAllText(Application.persistentDataPath + "/SaveFile.json"));
         }
         else
         {
             Debug.LogError("Cannot load game data");
         }
       
 
 
 
     }
avatar image Sabre-Runner Kek_Kek · Dec 15, 2018 at 06:44 AM 0
Share

Explain. How are you using this? Where? When? Did you check that all the data is serialized and deserialized correctly? Because you are not using just primitives there.

avatar image Kek_Kek Sabre-Runner · Dec 15, 2018 at 03:57 PM 1
Share

Well, I removed the SaveSettings and LoadSettings and I replaced it with PlayerPrefs. In the save file I added a PlayerPrefs.SetInt and in the enemy script when it's checking in the int is 0 or 1 I changed it to if(PlayerPrefs.GetInt == 1) and now it works.

Show more comments
avatar image
0

Answer by Vega4Life · Dec 14, 2018 at 03:26 AM

A scene load would cause it to reset. Is that intentional?

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 Kek_Kek · Dec 14, 2018 at 03:33 AM 0
Share

Yes, the scene loading is intentional.

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

98 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

Related Questions

Issue with PlayerPrefs not loading a value, as well as a bug... 1 Answer

How do I increase an integer?? 3 Answers

Damage script not changing enemy's health integer 1 Answer

How do I create a save scene feature though int value in c# 2 Answers

How to i set the int to hitpoints / 10 + the current int. 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