• 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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



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

Distribute terrain in zones 3 Answers

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

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges