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 Quiet · Mar 12, 2012 at 09:34 PM · variablesstaticglobal

My static variable is not changing

I'm just trying a simple test to get my static variable to change. I believe static variables are global variables and should be changeable from other scripts but it's not working out for me.

In one object I have a script that sets up the variable.

static var score: int = 0;

And in a different object and script I try to reference it.

var SCORE :int = GetComponent(S_Player).score;


function OnTriggerEnter(other: Collider){

  if (other.gameObject.tag == "Fish"){
          other.transform.position.y = 8;
          other.transform.position.x = Random.Range(-7,7); 
       //check if  asset is there...         
                    if (P_Death){
          Instantiate(P_Death,transform.position,transform.rotation);        
          SCORE+=1;             
                    Debug.Log("Works");
          }         
                   Destroy(gameObject);         }

}

Sorry the code looks crummy the blockcode doesn't seem to like formatting. Anyway, when I hit an object I should add 1 to my score. Well, when I run the function and I hit the object everything works but the adding the score part. I don't get any errors, and my score doesn't change. My debug log works so my function is working. So I'm guessing this is a logic problem so what did I do wrong? Is it just poor referencing and if so what is a better method? So yes, my whole function works, I have no errors messages but I'm not adding to my score.

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
0
Best Answer

Answer by mpavlinsky · Mar 12, 2012 at 09:43 PM

When you set an integer equal to another integer like that it does a copy by value so your integer score is equivalent to SCORE when you initialize it, but they point to different locations in memory so changes in one won't be seen in the other. If you want to change a simple static int like that and have your changes get reflected everywhere you either need to change that specific variable (S_Player.score) or you'll need to wrap it in a class.

It looks as though you are attempting to use the variable as a public and not a static though. You could access your static variable by simply using S_Player.score, you don't need to find the specific component of that class you have since a static variable is shared across all instances of that class. Your code the way it is written now would still compile and run if you made the variable in question public instead of static, of course it would have the same outcome as before because of the issues I outlined in the first paragraph.

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 Quiet · Mar 13, 2012 at 12:15 AM

So my GetComponent(S_Player).score; is pretty useless then? Awe. I thought it allowed me to change the variable, but it basically just gives me a copy right?

I don't understand exactly what you said, what would be the right class to use if I want to change that specific variable through different scripts?

Could I just make my variable a public static variable so there is only one variable used by all instances?

Comment
Add comment · Show 3 · 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 mpavlinsky · Mar 13, 2012 at 12:55 PM 0
Share

Firstly, please comment on my answer rather than post another answer, and please remember to mark my answer as accepted if it's sufficient.

You are exactly right that you want a public static variable. Then you would just lose the GetComponent call and access and modify it via S_Player.score.

As for the class thing, I'm not sure how this works because I work exclusively in C# but there are basically only references to classes in C#. So when set a class variable equal to another one they just point at the same data rather than having a copy of the same data. I'm not sure this is the most efficient way to do this, nor would it be my suggestion but just to show what I mean (sorry for the formatting, it looks good in the text field but I guess your not supposed to put code in comments):

public class Wrapper { public int data = 0; }

int data1 = 0;

int data2 = data1; data2 = 1; Debug.Log( data1 + " | " + data2 ); // 0 | 1

Wrapper wrapper1 = new Wrapper(); Wrapper wrapper2 = wrapper1; wrapper2.data = 1; Debug.Log( wrapper1.data + " | " + wrapper2.data ); // 1 | 1

avatar image mpavlinsky · Mar 13, 2012 at 12:58 PM 0
Share

Sorry for the bad formatting, but it's UnityAnswers fault. Everything looks good when I'm editing.

avatar image Quiet · Mar 14, 2012 at 02:20 AM 0
Share

Sorry, I'm new to this site and didn't realize I posted an answer ins$$anonymous$$d of a comment. Sorry about that. I have not done much with C# so that doesn't much, but it seems like this should not be this complicated, so I'll look more into it later. Thank you for your help though.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Global Variables Refuse to Cooperate 1 Answer

Not asigned health script 1 Answer

Changing static variables from another script? 1 Answer

Static variable issue in published 1 Answer

Global variables help 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