• 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 Z0hann · 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 Z0hann 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.

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

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

Related Questions

How to Change a variable in another script when ontriggerenter 0 Answers

making a variable (var) false when not colliding with a trigger. 1 Answer

update() keeps getting called with variable's old (initial) value, why wont the variable keep updated? 1 Answer

getComponent not working with Triggers? 1 Answer

How do I track an object of a specific type at collision? 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges