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 gaggedegg · Jun 20, 2019 at 09:00 AM · unity 2dvariableontriggerentertrackingscene change

Don't change the variable value every time you load script

Hey there !

I'm new to unity and i'm trying to load scenes based on items collected. Problem is that the counter is not counting my acquired items. I'm using OnTriggerEnter2D() to trigger the event; Below is the snippet:

private void OnTriggerEnter2D(Collider2D collision) { if (collision.gameObject.CompareTag("Player")) { collectionNumber += 1; Destroy(gameObject); if (collectionNumber == 1) { collision.gameObject.transform.Find("Acquired_Items").GetChild(0).gameObject.SetActive(true); qiCollector.gameObject.transform.GetChild(0).gameObject.SetActive(true); } else if (collectionNumber == 2) { collision.gameObject.transform.Find("Acquired_Items").GetChild(1).gameObject.SetActive(true); qiCollector.gameObject.transform.GetChild(1).gameObject.SetActive(true); } else if (collectionNumber == 3) { collision.gameObject.transform.Find("Acquired_Items").GetChild(2).gameObject.SetActive(true); qiCollector.gameObject.transform.GetChild(2).gameObject.SetActive(true); } else { Debug.LogWarning("All Items Collected !!"); } cN.text = "Collection Number " + collectionNumber.ToString(); } }

Whenever a new scene is loaded this script is loaded because it is on my quest item. And for every scene there is a quest item. So what I want to do is basically keep track of my collectionNumber, but it resets to 0.

Any help is much appreciated :)

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
1

Answer by $$anonymous$$ · Jun 20, 2019 at 09:41 AM

I think you have two different approaches to achieve this:

  1. You either write it to a file via some encoding - so players cannot modify the file easily - at the end of every scene and the start of every screen you read the file, decode it and store it in your variable. In long term you'll have to do this anyways since if someone closes your application you'll loose the variable unless you saved it somewhere outside of the program

  2. You make a class for e.g.: Globals, declare static variables and in its Awake or Start method you call the DontDestroyOnLoad() method on the GameObject itself. This method preserves your GameObject - and the attached scripts with its variables as well - even if a new scene is loaded. For more information, visit this link: https://docs.unity3d.com/ScriptReference/Object.DontDestroyOnLoad.html

This is rather an old video but you can get the idea from here: https://www.youtube.com/watch?v=tW_ADGMdiko

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 gaggedegg · Jun 20, 2019 at 10:00 AM 1
Share

I used Singleton for the purpose and it is working as I expected to be. Sure ! I'll try this solution in future problems like these. These Link is helpful: Singleton in Unity

Thank you for the Answer :)

avatar image $$anonymous$$ gaggedegg · Jun 20, 2019 at 06:30 PM 0
Share

That's a good approach as well, although I read it some time ago that it's not the safest method. For now, it surely looks like it was the best solution, but I'd advise you that in the future if your game gets more serious, look into coding and encoding so you can write your own save file :) Btw, if you were satisfied with what I wrote I would appreciate if you accepted my solution as an answer but only if you feel like!

avatar image
0

Answer by killop1997 · Jun 20, 2019 at 09:37 AM

You could set the values on a game object that has dontdestroyonload() method, it could be your player or a game manager of some sort, it depends how you write your code, but if you exit the game the data will be lost. You can also save and load the data, here is a link to a good tutorial on that Preserving Data.

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 gaggedegg · Jun 20, 2019 at 09:59 AM 0
Share

I used Singleton for the purpose and it is working as I expected to be. Sure I'll try this solution in future problems like these.

Thank you for the Answer :)

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

115 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple objects sharing a boolean variable in a script. 1 Answer

How can I detect a collision between two clones and delete them both? 1 Answer

what is going on(grapping hook) 0 Answers

Bomb not destroying colliders 0 Answers

How to Change a variable in another script when ontriggerenter 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