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 Irsan · Jul 01, 2011 at 02:30 AM · variablecomponentmathbce0020

Storing transform information in a variable?

I have a cube in my scene, and I want to get the x-scale in the form of a global variable. I used this:

    static var BrickScaleX = (transform.localScale.x);


However, it doesn't work. This is the error message it gives me:

"Assets/GetScale.js(1,27): BCE0020: An instance of type 'UnityEngine.Component' is required to access non static member 'transform'."

I want to be able to perform math functions on the x-scale of the cube, but I can't do it this way.

Comment
Add comment · Show 2
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 Dreamblur · Jul 01, 2011 at 02:37 AM 0
Share

Is your script derived from $$anonymous$$onoBehaviour?

avatar image aldonaletto · Jul 01, 2011 at 03:52 AM 0
Share

In Unity, each script instance is a fresh copy of the original, including its public variables, while static variables are unique, like any old and good global. If you will have only one cube in your game with this script, the variable may be declared static, and thus be accessed by any other script. But if you may have several cubes, each one with its own brickScaleX, DON'T declare it static, or all cubes will write their localScale.x values in the same variable - do it like in the @Eric5h5 answer.

1 Reply

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

Answer by Eric5h5 · Jul 01, 2011 at 02:39 AM

Only declare variables outside functions; all actual code should be inside functions. Static doesn't mean global; only add static if you actually want only one instance of it per class. If it's public (which it is by default in JS), then it's global. Also, it makes things easier to follow if you follow the convention of using lowercase for variable names, and uppercase for function and class names.

 var brickScaleX : float;
 
 function Start () {
     brickScaleX = transform.localScale.x;
 }
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 aldonaletto · Jul 01, 2011 at 03:38 AM 0
Share

Static vars in unity are the equivalent of global variables in the old plain C (and other languages): Global variables have program scope, which means they can be accessed everywhere in the program, and they are only destroyed when the program ends

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Accessing a variable of a script from another scene. 7 Answers

How can I access a GameObjects components using an instantiation variable? 4 Answers

[CLOSED] An object reference is required to access non-static member 0 Answers

How to compare variable component types between GameObjects at run time? 1 Answer

Accessing components via script and assigning randomized values to their parameters? 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