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 tylermakes · Jan 09, 2016 at 12:18 PM · c#programming-basics

Variables not updating

I feel like I'm clearly missing something here. I've got a very simple class that moves an object forward. I'm trying to keep track of its lifespan, so I'm incrementing an int every update. However, when I try to call GetLifeSpan() that value is zero. What's even weirder is that, if I print the value in the Update function, it also appears as 1 (as if it was just updated from zero). Also, the "wait" int, which is incrementing correctly and prints correctly in Update() appears as zero in GetLifeSpan().

What am I missing here? I'm so confused.

     private int wait = 0;
     private Vector3 movement;
     public int lifespan = 0;
 
     // Use this for initialization
     void Start () {
         movement = transform.forward;
     }
     
     // Update is called once per frame
     void Update () {
         lifespan = lifespan + 1;
 //        print (lifespan);
 //        print (wait);
         if (wait == 0) {
             wait = 20;
             //print (movement);
             transform.Translate(new Vector3(0,0,0.05f));
 
         } else {
             wait--;
         }
     }
 
     public int GetLifeSpan() {
         print ("getting ls");
         print (lifespan);
         print (wait);
         return lifespan;
     }

Alternatively, am I screwing something up in the code that spawns it? The above code is in the BulletCode class, and the below code is basically the gun that fires it. Also, can anyone explain to me why bc = bullet.transform.GetComponent(BulletCode); doesn't work (it has an error about it not being a System.type... I've seen other code that looks to be doing this though).

     private int test;
     public GameObject bullet;
     public BulletCode bc;
 
     // Use this for initialization
     void Start () {
         test++;
         MakeBullet ();
     }
     
     // Update is called once per frame
     void Update () {
         //print (bc.getLifeSpan());
         if (bc.GetLifeSpan() > 10) {
             // never gets called
             Destroy(bullet);
             MakeBullet ();
         }
     }
 
     public void MakeBullet () {
         Vector3 pos = transform.position;
         pos = pos + new Vector3 (0f, 0.3f, 0f);
         Instantiate (bullet, pos, transform.rotation);
         bc = bullet.transform.GetComponent("BulletCode") as BulletCode;
         //bc = bullet.transform.GetComponent(BulletCode);
     }
Comment
Add comment · Show 3
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 ShadyProductions · Jan 09, 2016 at 02:29 PM 0
Share

try lifespan++;

avatar image tylermakes · Jan 09, 2016 at 02:55 PM 0
Share

I've already tried lifespan++; No change.

avatar image TheRealGrendel · Jun 17, 2016 at 11:39 PM 0
Share

Did you ever solve this? I am having the same exact problem, and no amount of Googling and asking has solved it. Thanks!

4 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by allenallenallen · Jan 09, 2016 at 12:31 PM

Script seems fine to me. Make sure you saved the code in your script editor and that Unity had the time to compile it. THEN you can try clicking play again.

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 tylermakes · Jan 09, 2016 at 03:02 PM 0
Share

It's definitely compiling. Any change I make, however small is taking effect.

avatar image
1

Answer by JoshuaMcKenzie · Jan 09, 2016 at 03:11 PM

sounds like you have a different source thats affecting the value (or you're constantly instantiating a new instance per frame, but that should have been obvious to you), try turning the value into a property and print to console when the value is changed,

 private int _lifespan = 0;//backing variable
 public int lifespan
 {
     get
     {
         return _lifespan;
     }
     //make it private set so that the variable is read-only
     private set
     {
         _lifespan = value;
         Debug.Log(value);
     }
 }

with a property you can track any access to the variable. And benefit of the Debug.log is that it'll get you a stacktrace to see if the value is getting changed elsewhere.

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 tylermakes · Jan 09, 2016 at 03:25 PM 0
Share

Good point. I'll try that out. It's definitely not re-instantiating because I can see the object moving across the screen (re-instantiating should keep making a new one at the spawn).

avatar image tylermakes · Jan 09, 2016 at 03:36 PM 0
Share

I did that test, and I'm pretty sure this is a failure to get the BulletCode component from the object correctly. I can see the get/set values incrementing, but there's another get that's co$$anonymous$$g from the call in the gun that's always returning zero.

avatar image
0

Answer by richardgengle · Aug 07, 2020 at 03:14 AM

i am having similar problems... but for your case,,,, how is bc.getlifespan working... i dont see the connection there

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 mannysayah · Feb 20, 2021 at 10:29 PM

You need to reset the Script Component in Unity to see changes.

To do that, click on the ellipsis icon (shown below) and then click Reset. The inspector will now show the new values.

Component settings


setttings.jpg (9.4 kB)
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

8 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

[ANSWERED] c# performance impact - Global variables vs Local variables 1 Answer

(SOLVED) Accessing (string) arrays by enumerations from another class 0 Answers

I need help in understanding this: room.transform.position.x 1 Answer

I have a serious problem right here every time I write or copy a c# code I keep getting errors everywhere it says not supported in unity engine. 2 Answers

Move the objects Y? 0 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