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 FinKone · Jun 13, 2013 at 01:08 AM · c#beginnerreferencenonstatic

Refin' another script in relation to speed, and not tacking from using the Update?

Ok guys - so I almost ripped my own head off looking at the ref manual trying to figure out how to do something that would have been rather small but fundamental...

That is referencing a value from another script. I have my pathfinding in one script, and my animations in another. The main reason I'm doing this is to expose myself to learning in regards to referencing effectively rather then just lumping it all into one script or doing retarded calls per frame to check a value.

The float speed I have set up is in the pathfinding script - on the same object. The animation script I reference under

Not the actual code - closed out Unity already but I've been looking at this little script for a bit so memory should do.

 AnimationScript.cs
 
 private float isMoving;
 
 Start()
 //DAAMMMMNNNN YOOOUUU.
 Path path = GetComponent<Path>();
 isMoving = path.speed;
 
 Update()
 if isMoving >= 3
 animate
 else stop animation

Now... it won't stop animating. I can understand why (maybe - I've only been learning coding for 7 months or so). I still have a lot to learn and try to keep my head in the refs rather then copy and pasting codes because ya' aint gonna learn crap doin' that.

So here is where I need help.

Is this valid logic on my part? That since I'm only checking the path.speed once on start, it will stay a static value? (path.speed is not a static, it is a public float in path.cs) but static in the sense to animation.cs because it will NEVER check it again?

Its gotta be bad performance to have it checking in even Fixed - so is there a way to for me to send a message TO animation rather then calling from it if speed is over say 3? That seems the more logical way.

I'm going to sleep either way and gonna sleep on it but man guys I just had one of them days where something so simple makes ya' wanna pull your own hair out...

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

Answer by aldonaletto · Jun 13, 2013 at 01:21 AM

You should read path.speed every Update, or else the saved value will remain the same. Instead of reading path.speed once at Start, get the path reference in a member variable (variable defined outside any function, which retains its value while the script exists):

 Path path; // declare this variable outside any function
 
 void Start(){
   path = GetComponent<Path>();
 }
 
 void Update(){
   if (path.speed >= 3){
     // animate
   } else {
     // stop animation
   }
 }

Accessing a variable through a reference is basically as fast as accessing a local variable, thus you won't have any performance loss.

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

15 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

Related Questions

Where i can find detailed description of Unity C# funtions? 1 Answer

Error when referencing one script from another. 1 Answer

How to reference another script and call a function in C# ? 2 Answers

Trying to access a variable from another script attached to same object. 1 Answer

Multiple Cars not working 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