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 /
  • Help Room /
avatar image
0
Question by squadO · Sep 24, 2018 at 06:04 PM · unity 5scripting problemvariableproblema

Getting wrong value of variable

HEY, HOW'S IT GOING, BUDDYS?
I need some your help today. All in all, I have 3 main scripts: globalvariables.cs (where I save all my global variables), onTreeClicked.cs (event when I click on button) and CoinExchange.cs (also event when I click on button). All other scripts doesn't really have a matter.
alt text


I'll try explain what they do. In my first script (globalvariables.cs) I save all my global varibles:


globalvaribles.cs

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 
 public class globalvariables : MonoBehaviour {
     public int Wood;                // Creating a Wood variable
     public int Coin;                // Creating a Coin variable
 }
 


Then I take them to my second script (onTreeClicked.cs). I'mma left screenshot with comments what every line do.
I add 1 to Wood a click. And as I can understand it changes it's value from default. (in my globalvariables I dont say that Wood = 0 or smth. So I reckon that when I add 5 to Wood it equals 5.)

onTreeClicked.cs

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 
 public class onTreeClicked : globalvariables {          // Getting all variables from globalvariables script
 
     private Shake shake;                    // Variable for Camera Shaking
     private Hit hit;                        // Varible for hit
     private ParticleSystem woodeffect;      // Wood icon appears above the tree
     private ParticleSystem pluseffect;      // 'plus effect' next to wood icon
     private Text woodcount;                 // Wood count text
     private Text coincount;                 // Coin count text
 
     void Start () {
         shake = GameObject.FindGameObjectWithTag("ScreenShake").GetComponent<Shake>();                      // Getting Components//
         hit = GameObject.FindGameObjectWithTag("Player").GetComponent<Hit>();                               //*******************//
         woodeffect = GameObject.FindGameObjectWithTag("woodeffect").GetComponent<ParticleSystem>();         //*******************//
         pluseffect = GameObject.FindGameObjectWithTag("pluseffect").GetComponent<ParticleSystem>();         //*******************//
         woodcount = GameObject.FindGameObjectWithTag("woodcount").GetComponent<Text>();                     //*******************//
         coincount = GameObject.FindGameObjectWithTag("coincount").GetComponent<Text>();                     // Getting Components//
 
 
         woodcount.text = Wood.ToString();           // Display start Wood value
         coincount.text = Coin.ToString();           // Display start Coin value
         pluseffect.Stop();                          // Stoping plus effect
         woodeffect.Stop();                          // Stoping wood effect
         
     }
 
 
     public void OnButtonTapped()                    // onClick function
     {
         shake.camShake();                           // Shake animation activated
         hit.charHit();                              // Hit animation activated
         Wood++;                                     // Add 1 to Wood variable
         woodcount.text = Wood.ToString();           // Show new value of wood
         woodeffect.Play();                          // Playing wood effect
         pluseffect.Play();                          // Playing plus effect
         
 
 
     }
 
 
 
     void Update () {
         
     }
 }

alt text



In my third script (CoinExchange) I wanna exchange wood for coins (1 coin = 5 wood). But when I tryna get Wood varible from onTreeClicked.cs (that must equal some value) I get 0. I mean, when I'm checking Wood variable it equals 0.


CoinExchange.cs

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 
 public class CoinExchange : onTreeClicked {             // Getting variables from onTreeClicked.cs 
 
     private Text coincount;                 // Coin count text
 
     void Start (){
         coincount = GameObject.FindGameObjectWithTag("coincount").GetComponent<Text>();                     // Getting Components//
     }
 
     void Update (){
         
     }
 
     public void Exchange()                  // onClick event
     {
         Debug.Log(Wood);                    // Checking Wood value        
     }
 
 
 }
 


Guys, I'm asking for your help. Every answer will be appreciated.

scripts.png (37.3 kB)
screenshot-1.png (68.5 kB)
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

1 Reply

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

Answer by squadO · Sep 24, 2018 at 06:22 PM

I have found solution. I only need to use static variable.

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

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

I need help working out a C# script with error CS0120 1 Answer

Score value in GameOver screen is 0 instead of final value from the Game Level scene(used PlayerPrefs), how do I display the final value from Game Level scene in the GameOver scene? 1 Answer

change material color for all object -Element object 1 Answer

How would I make a variable equal a variable but only its past value? 1 Answer

Why is my Camera not rotating properly? 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