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 /
avatar image
0
Question by dah41kj · Mar 12, 2020 at 10:49 AM · objectvariablevalueupdatingobject-oriented-programming

Can't update value of var in another class

I have variable 'capacity' which is updating normally, but I can't get an updated value in another class, but initial value is there. Help me :<

 public class housingPanelScript : MonoBehaviour
 {
     public Housing houseObj;
     public TextMeshProUGUI capacity_txt;
     public int capacity = 10;
 
     void Update()
     {
         capacity_txt.text = "max capacity: " + capacity.ToString();
     }
 
   // some code
 
     public void upgradeHouse()
     {
         capacity += 10;
         Debug.Log("capacity " + capacity);
     }
 }

// Score is updating btw (ontriggerenter2d method)

 public class Spawn : MonoBehaviour
 {
     public GameObject man;
     int spwCount = 0;
     public housingPanelScript house;
     public Destroyer score;
 
     public void spawn()
     {
         if (score.i < house.capacity && spwCount < house.capacity)
         {
             Instantiate(man, new Vector2(-1, 1), Quaternion.identity);
             spwCount++;
         }
         else
             Debug.Log("not spwn " + house.capacity + " " + score.i);
     }
 }

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 · Mar 12, 2020 at 11:16 AM 0
Share

How do you call 'upgradeHouse' and are you sure the public housingPanelScript house; is assigned to the correct script instance in your scene?

avatar image dah41kj ShadyProductions · Mar 12, 2020 at 11:45 AM 0
Share

Yes, value is updating (in the original class). I can see it from debug log. I'm calling this method simply through clicking on a button.

avatar image dah41kj ShadyProductions · Mar 12, 2020 at 11:56 AM 0
Share

Figured it out. Deal was in a prefab. When I manually drop a housingPanelScript object to a Spawn script it started updating. But how I can assign a prefab there as long as it either started updating?

1 Reply

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

Answer by Frodo_21 · Mar 12, 2020 at 12:19 PM

As I can see it you will get a "NullReferenceException", right?

In your "Spawn" script add the "Start()" Method and than find the GameObject, where the Script is added and find the Script on this GO.

    void Start()
     {
         house = GameObject.Find(/*Tag of GO*/).GetComponent</*Your scripts name*/>(); 
     }
Comment
Add comment · Show 4 · 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 dah41kj · Mar 12, 2020 at 12:42 PM 0
Share

It doesn't but thanks for that tip about getcomponent, didn't know about it

avatar image Frodo_21 · Mar 12, 2020 at 12:59 PM 0
Share

Np, but it's not solveing your problem. So if I get it right: You have more than one house and want to Instatiate a $$anonymous$$an as long as there is enough space in each house?

avatar image Frodo_21 · Mar 12, 2020 at 01:03 PM 0
Share

And you want to get the capacity of a house when it's been instantiated ?

avatar image dah41kj Frodo_21 · Mar 12, 2020 at 11:09 PM 0
Share

Yes, u get it right, but it solved my problem for now and worked as it should. $$anonymous$$b I need to simplify my thoughts and stay it as it is now.

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

136 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

Related Questions

global variable need advice 1 Answer

Difficulties Clearing/Updating A Variable 0 Answers

Scriptable object keeps referencing the objects? 1 Answer

Variable value not changhing 3 Answers

Variable value not changing (bug or i'm just an idiot?) 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