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 /
This question was closed Jul 18, 2017 at 01:36 PM by agencebrunel for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by agencebrunel · Jul 18, 2017 at 03:33 AM · positionvar

Store x y z position of gameobject in var.

Hello :)

For my script donjon room generator, i need to calculate the space between the doors of my rooms. - I need to store in separated var the position of every room.

I have try this but return always 0 :

 private var xadresse : float;
 private var zadresse : float;
 
     function Update () {
         var xadresse = transform.position.x ;
         var zadresse = transform.position.z ;
     }
     function OnGUI ()
     {
         GUI.Box(Rect(60,200,130,50),"x :" + xadresse);
         GUI.Box(Rect(60,250,130,50),"z :" + zadresse);
     }

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

  • Sort: 
avatar image
2
Best Answer

Answer by Cornelis-de-Jager · Jul 18, 2017 at 04:36 AM

Within your update function, you are creating new variables. Then immediately throwing them away. Do the following:

private var xadresse : float; private var zadresse : float;

  function Update () {
      xadresse = transform.position.x ; // Remove var
      zadresse = transform.position.z ; // Remove var
  }
  function OnGUI ()
  {
     GUI.Box(Rect(60,200,130,50),"x :" + xadresse);
      GUI.Box(Rect(60,250,130,50),"z :" + zadresse);
  }

The reason for this is you create a new variable each time you use

var so best to only use it once.

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 agarcialeon · Jul 18, 2017 at 12:46 PM 1
Share

This is correct. As @Cornelis-de-Jager says, you're creating new variables called 'xadresse' and 'zadresse' each time the Update function is called. This is because in Unityscript (as in Javascript) uses var keyword to create a new variable in the scope you are defining it.

This is, that if you write : var myVariable

inside a function like Update, your variables are being defined only inside that function and if you write them outside any function (i.e. the global scope) these variables can be accesed inside the rest of the functions but any variable defined with the same name inside a function will "hide" the global variable.

Follow this Question

Answers Answers and Comments

74 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

Related Questions

How can I add text at a point where an enemy dies? 3 Answers

Camera rotation around player while following. 6 Answers

this code doesnt work when the buttons anchor are not same(custom anchor) 0 Answers

Changing the rotation of a Trigger makes the Trigger not work anymore? 1 Answer

Why Do My Obstacles Do This In My Object Pooler? 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