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 jonm · Sep 07, 2011 at 10:43 PM · javascriptvariables

Javascript question.

Ok so I know this may seem like a novis question but I just can't seem to grasp variables. I get functions and all that but just can't get my mind around variables. I know it's like an empty box ready to be filled with data for easy access, but isn't that almost the same as a function? Does anyone have a good way to explain this to make it easier for me to understand or know of good script snippets showing what the clear purpous of a function is.

any help would be much appritiated. THANKS!

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Spartan301 · Sep 07, 2011 at 10:50 PM

A function is not a way to store data, it stores a section of code so that it can be used again repeatedly with ease. For example if I have a gameObject that I want to move to a new location each time it is hit, then I would create some code that would do that, and place that code inside a function, for example, function ResetPosition(){...}. Then whenever I needed to reset the objects position, I could just write ResetPosition(), instead of writing the entire code segment again. Hope this helps. In short, variables are meant to store small data pieces, like a number or a word, while functions store and execute sections of code. Hope this helps.

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
avatar image
0

Answer by aldonaletto · Sep 07, 2011 at 11:32 PM

A function does something, while a variable stores something (a function may also return a value, but that's another story - don't worry about this now). Variables usually have a type: int holds only integer values (1, 5, 432), float holds real values (1.0, 0.2, 3.14159), String holds characters and strings ("a", "hello", "Bye"), and the Unity types can hold references to Unity components or objects (like Transform, Rigidbody, Collider, Camera etc.)

There's an example of a function and a variable:

var speed: float = 60.0;

function Update(){ transform.Rotate(0, speed Time.deltaTime, 0); } The variable speed declared at the top stores a float number - the rotation speed, in this case.
The function Update is called by Unity before drawing each frame. We usually place inside it code that updates the screen or that must execute frequently. In this case, I called the transform function Rotate, which rotates the object around the axes x, y and z by the corresponding values in degrees: x = 0, y = speed
Time.deltaTime, z = 0, in this case. Notice that the object should rotate around the Y axis, and at speed degrees per second. Since Update is executed before every frame, Unity supply a special variable to help us: Time.deltaTime is the time elapsed since the last frame was rendered, and works as a "correction factor": when multiplied by speed it ensures the rotation will be constant and equal to speed rotations per second.

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 jonm · Sep 08, 2011 at 03:21 AM 0
Share

Ok, I have a fairly good grip on functions now. I also get the variable types, but I still don't get what practical use a variable has. Or I could just making this harder than it really is. What i'm getting now is that it just makes a substitute. Like in your example

var speed: float = 60.0 In basic terms it just means that whenever you right down speed it actually means 60.0?

var projectile : Rigidbody; var speed = 20;

function Update () {

if ( Input.GetButton ("Fire1")) {

clone = Instantiate(projectile, transform.position, transform.rotation); clone.velocity = transform.TransformDirection( Vector3 (0, 0, speed));

Destroy (clone.gameObject, 3);

}}

This script confuses me a little, if someone could explan this it will hopefully snap into my brain.

THAN$$anonymous$$S!

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Can someone help me fix my Javascript for Flickering Light? 6 Answers

Setting Scroll View Width GUILayout 1 Answer

What variables can i declare? 1 Answer

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

How to access a non static variable by another static variable? 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