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 Evil_Weevil · Nov 03, 2013 at 07:23 PM · function update

Vars from different functions

A rather general question.

I have a var in my OnMouseDrag function. I calculate it, it works.

Then I refer to it in my OnMouseUp — it says it doesnt know what I am talking about.

I guess it has something to do with the function type...

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 LukaKotar · Nov 03, 2013 at 07:26 PM 0
Share

Could you edit the question and post some of the code?

avatar image mattssonon · Nov 04, 2013 at 08:25 AM 0
Share

Do you know what scope is in program$$anonymous$$g terms?

avatar image Evil_Weevil · Nov 04, 2013 at 09:39 AM 0
Share
 public var object : Transform;
 
 function On$$anonymous$$ouseDrag () {
    player = GameObject.FindWithTag("Player").transform; 
    transform.LookAt (player);
    public var distance : float = Vector3.Distance(object.transform.position, player.transform.position);
    
    
 }
 function On$$anonymous$$ouseUp ()
 {
 rigidbody.AddForce(Vector3(distance * 50,distance * 40,0));
 
 }



here`s my code; I don`t know what scope is in progra$$anonymous$$g terms.

1 Reply

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

Answer by Cains · Nov 04, 2013 at 09:51 AM

Your problem is indeed the scope of your variable distance. Because you declare the variable in the OnMouseDrag() function, the variable only exists within that function. So once that function ends the variable basically doesn't exist.

 public var object : Transform;
 public var distance : float;
 
 function OnMouseDrag () {
     player = GameObject.FindWithTag("Player").transform; 
     transform.LookAt (player);
     distance = Vector3.Distance(object.transform.position, player.transform.position);
 }
 function OnMouseUp () {
     rigidbody.AddForce(Vector3(distance * 50,distance * 40,0));
 }

This should work for you. By declaring distance outside the functions you can now use it anywhere in that script.


Another tip is it's not good practice to name variables with names used by the engine. Although I don't think it's actually causing you any problems, you declared a variable named object which is also a type of variable (that's why it's in blue), you should probably name it something else.

Also, I see that player is never declared but I'm sure it is in your actual code. How you have it now everytime they drag the mouse it's going to assign the Player GameObject to player, which will mean it will happen many times even though you only need to do it once. You should declare it in Start() like below

 public var player : Transform;
 
 function Start() {
     player = GameObject.FindWithTag("Player").transform;
 }

Finally, I'm fairly sure you don't need to declare your variables as public as this is done by default, so you only need to declare them as "var distance : float;".

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 Evil_Weevil · Nov 04, 2013 at 10:08 AM 0
Share

very nice answer, thank you. everything worked fine.

avatar image mattssonon · Nov 04, 2013 at 10:09 AM 1
Share

Please accept the answer as the correct one then.

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

17 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

Related Questions

Could someone fix this : It says expressions must only be executed ofr their side-effects 1 Answer

How to access variable from another function? 2 Answers

Function not executing 1 Answer

Health Regeneration Issues. 2 Answers

How to track an object per Frame? 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