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 99andy · Sep 14, 2013 at 02:27 PM · variablefunctionclasses

Problem with increasing a variable

I have a problem with increasing an integer variable (level) by one. I think, I know where the problem is, but I do not know how to fix it. I explain my problem in code:

Let's say, I have two classes, Game.js and Menu.js.

In menu class I have a detection for buttons, like play, score and so. After score button is clicked, I call a function from game class. The function increases my level variable (there is a line: level++), but it does not increase it by one, but by two times. It performs a given piece of code twice, but I clicked just one time.

Then I try to put the function in menu class, and there it performs just one time, if I wanna. Of course, in Hierarchy have game objects with both classes. Here is sample of code:

Menu.js

 //declare the raycast objects here so we dont need to instantiate them each frame
 private var ray        : Ray;
 private var rayCastHit : RaycastHit;
 
 var game : Game;        // instantiate class Game.js
 
 var play : GameObject;  // play button
 var score: GameObject;  // score button
 var exit : GameObject;  // exit button
 
 function Start()
 {
 }
 
 function Update()
 {
     var aniPlay : aniSprite = GetComponent("aniSprite");
 
     if(Input.GetMouseButtonDown(0))
     {
         ray = Camera.main.ScreenPointToRay(Input.mousePosition);
         
         if(Physics.Raycast (ray, rayCastHit))
         {
             if(rayCastHit.transform.name == "playButton" && !variables.drawAbout)        
             {
                 Application.LoadLevel("GameScene");
             }
             if(rayCastHit.transform.name == "scoreButton" && !variables.drawAbout)        
             {
                 game.write();
             }
             // here is more button detections, but this is just a sample
         }
     }
 }


Game.js

 function Start()
 {
 // in my original code, I give here valus to variables
 }
 
 function Update()
 {
 }
 
 function write()
 {
     // Here is the root of the problem, this log will be write twice, but I want to only one time, 
     // because I have here the level variable and this increase it twice, not one time 
     Debug.Log("this log will be write twice");
 }


If I have given the function write() to class Menu.js, all have been right, but I can't give every funtion to one class, it would be such mess. Please, help me, I really do not know, how to increse the variable of another class via a function only once.

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 JoaquinRD · Sep 14, 2013 at 02:53 PM 0
Share

Do you have two $$anonymous$$enu scripts/objects for some reason?

1 Reply

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

Answer by JoaquinRD · Sep 14, 2013 at 03:35 PM

Sounds like you have two objects that each have a Menu.js script component. Both are calling the Game.write function when you click the score button.

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

16 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

Related Questions

When issuing to print a private variable from the script, the script prints all the variables attached to objects using the same script instead of the specific object. How do I fix this? 3 Answers

How to use different types of scripts with an override function. 2 Answers

Editting a custom javascript class in the inspector 1 Answer

Making a var appear on click 1 Answer

Issues referencing variables between functions 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