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 FriTooTheEnd · Jan 18, 2019 at 02:23 AM · variablefloatpublic

Public Variable not updating

Recently I've hit a roadblock with my current project, where, in essence, I need a float to be accessible from other objects, while still being able to be updated in script, that way when another object calls upon it through Get Component, it gives the updated variable.

 //The Object Script that holds the variable
 public float distanceToPlayer
 public void calculateDistanceFromPlayer(){
     GameObject player = GameObject.FindGameObjectWithTag("Player");
     Vector2 playerPosition = new Vector2 (player.transform.position.x, player.transform.position.y);
     Vector2 tilePosition = new Vector2 (transform.position.x, transform.position.y);
     float distanceToPlayer = Mathf.Floor(Vector2.Distance (playerPosition, tilePosition));
 }

And the code that is accessing the above code

 // Closest possible to player
 float cptp = 0;
 public List <TileScript> selectableTiles = new List <TileScript> ();
 public List <TileScript> cptpList = new List <TileScript> ();
 
 foreach (TileScript TS in selectableTiles) {
     if (TS != null) {
         if (TS.distanceToPlayer < cptp) {
             Debug.Log ("Test One");
             cptpList.Clear ();
             cptpList.Add (TS);
             cptp = TS.distanceToPlayer;
         } else if (TS.distanceToPlayer == cptp) {
             Debug.Log ("Test Two");
             cptpList.Add (TS);
         }
     }
 }

I've been trying to look at different errors, and from what I've gathered, distanceToPlayer is not updating. The code for it works, but it doesn't update the objects code (I've used Debug.Log, and each tile has said how far away it is from the player, but when I call it from outside of calculateDistanceFromPlayer(), it states 0) It seems that I need it to be both a public, and not public variable at once, is their any way to force it to update from within the code? I appreciate any help on the mater, thank you in advance. -Sincerely, Eve

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

Answer by Cornelis-de-Jager · Jan 18, 2019 at 05:54 AM

You are creating a new Local variable with the same name as your public variable. So When you update it you only update the local variable - not the global one.

  public float distanceToPlayer; // <- this is the one being accessed
  
  public void calculateDistanceFromPlayer(){
     ...
     float distanceToPlayer = Mathf....; // This is a new local variable
  }

Solution:

 // Solution : Remove the float
 /* float */ distanceToPlayer = Mathf.Floor(Vector2.Distance (playerPosition, tilePosition)); 


Extended Solution

 public void calculateDistanceFromPlayer(){
      GameObject player = GameObject.FindGameObjectWithTag("Player");
      Vector2 playerPosition = new Vector2 (player.transform.position.x, player.transform.position.y);
      Vector2 tilePosition = new Vector2 (transform.position.x, transform.position.y);
      distanceToPlayer = Mathf.Floor(Vector2.Distance (playerPosition, tilePosition));
  }
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 FriTooTheEnd · Jan 19, 2019 at 01:37 AM 0
Share

Thank you for the help! I was without Internet, so I found another solution (I deleted the public variable, and told it to return the variable specifically), this helps explain why I didn't come across the problem again later when I thought I did the same thing as before.

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

105 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

Related Questions

Public Float Not Showing Up in Inspector 1 Answer

Can I make variables visible to other scripts without making them visible in the Inspector? 1 Answer

Invisible Public Variable in Inspector 5 Answers

Share Public Classes 1 Answer

Cannot access AudioSource from within function 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