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
-1
Question by pranav_prabhu · Jan 18, 2014 at 05:17 PM · variableresetvaluetextbox

How to keep variable value

I have a piece of code that I have attached to many game objects. In this script, the player is supposed to enter the meaning and gets 5 points for the correct answer.

 private var inRange: boolean = false;
 var myStyle = new GUIStyle();
 var score:int;
 public var mess: String = "Clue";
 var stringToEdit : String = "Remove this and enter meaning";
 var finalstring:String="nil";
 function OnTriggerEnter(other:Collider) 
 {
     if (other.tag == "Player")
     {
         inRange =  true;
     }
 }
 
 function OnTriggerExit(other:Collider) 
 {
     if (other.tag == "Player") 
     {
         inRange =  false;
     }
     if(score==5)
     {
         score=5;
     }
 }
 
     
 
 
 function Update(){
      if(finalstring==stringToEdit)
      {
          score=score+5;
      }
      if(score==5)
      {
          stringToEdit="null";
      }
 }
 
 function Start()
 {
     if(score==5)
     {
         score=5;
     }
 }
 
 function OnGUI () 
 {
     
      if(inRange)
         {
         GUI.Label (Rect (10, 10, 100, 20),"Score: " +score.ToString()); 
         // Make a text field that modifies stringToEdit.
         stringToEdit = GUI.TextField (Rect (10, 50, 1000, 20), stringToEdit, 25);
         GUI.Label (Rect (10, 30, 2000, 1000),"Clue: " +mess,myStyle);
         }
 }
 
 
 
 
 
 
 
 
 

The problem is that whenever the player moves on to the next object after getting 5 points, the score resets to 0. how to keep the score as it is

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

3 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Jamora · Jan 18, 2014 at 05:29 PM

It's not that the score resets on each Game Object, it's that you save the score on a single GameObject-basis. This means each GameObject has a different idea of what the score is.

To remedy this, the simplest solution is to use a static variable. So, instead of var score:int; have static var score:int;. Static means that the same value is shared between all instances of that class.

Several other things about your code, I feel need pointing out, are the if blocks on lines 43 and 21. Setting score to 5 if it already is 5 seems unnecessary to me. Further, your Update method will work better if it's:

 if(finalstring==stringToEdit)
 {
   score=score+5;
   stringToEdit="null";
 }

This way it'll change stringToEdit each time a correct answer is entered, as opposed to when the correct answer is entered and the score is 5 (causing the score to increase indefinately after the first correct answer).

One more problem I see with this design, is that the player is able to answer a question multiple times, each time adding 5 to score. The simplest solution is to use an isAnswered boolean flag, which is set to true within the above if clause and checked to be false before awarding any points.

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 Jocca · Oct 04, 2015 at 07:06 PM 0
Share

Thnks! Just what i was looking for (set the var to static to keep a simple collecting items counter working with multiple scenes) :)

avatar image
0

Answer by And0 · Jan 18, 2014 at 06:40 PM

As far as I can tell the problem is that each object has its own score variable and draws its own label.

I would create a separate static object containing a variable to handle the total score and to draw the label. Then each object could pass its score value over to that scoreCounter object (or whatever you want to call it) where it is then added to the total score.

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

Answer by bodec · Jan 18, 2014 at 07:12 PM

You need a solid gameobject to carry over from scene to scene at the start of each scene all previous gameobject are destroyed in a start functionon a gameobject you keep use DontDestroyOnLoad() I think is the command and you won't loose the data

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

22 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

Related Questions

int variable loses its value 0 Answers

How to return to a value used before? (C#) 1 Answer

I have a variable that does not re-set on the closing of my program, the variable is modified on the trigger enter, why does it not re-set? 0 Answers

How do I reset a variable? 2 Answers

assign value for all the script 2 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