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 Karsnen_2 · Dec 03, 2011 at 02:26 AM · javascriptguivariablesscriptingbasics

How to access a non static variable by another static variable?

Hello,

I wish to use a value in say B script. Now in A script I have a variable is modified using GUI in run time. I wish to use that same variable in another script in run time i.e. changes in GUI should affect both the variables.

Code :

 var canonPrefab : Transform;
 
 var velocity = 10.0f ;
 
 var angle : float = 20.0f;
 
 //Global variables
 
 static var g_angle : float = angled;
 
 static var g_velocity :float = velocity;


Could someone help me with this?

Thank you very much in advance.

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

Answer by jahroy · Dec 03, 2011 at 02:38 AM

If the above script is defined in a file named CannonController.js, you could refer to it in another script like this:

filename: SomeOtherScript.js

public var cannonScript : CannonController;

function Update () { if ( cannonScript == null ) { Debug.Log("Please assign CannonController object"); return; }

 Debug.Log("Cannon Script velocity: " + cannonScript.velocity);

}

When you define a public variable named cannonScript of type CannonController you can assign any object with a CannonController script attached to it in the Inspector.

You can do that like this:

  • select an object with a SomeOtherScript attached to it by left-clicking it
  • look in the Inspector for a slot named Cannon Script
  • drag any object with a CannonController script attached to it onto this slot

I'm not sure why you have those static variables. Perhaps you could explain that.

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 Karsnen_2 · Dec 03, 2011 at 10:50 PM 0
Share

It worked out pretty well. $$anonymous$$y perception was as I use a static variable I though I could alter or use it in another script too. But this method never stuck me. To elaborate I never knew that you create a variable of the type of another script. Thank you very much.

avatar image Karsnen_2 · Dec 03, 2011 at 10:56 PM 0
Share

And a small correction. The first line of your script. I think you tried to mention the name of the script. If so it is not "CameraController", it is "CannonController". Just for someone who might be looking into this answer for their own purpose in future.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Setting Scroll View Width GUILayout 1 Answer

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

What's wrong with my function? It keeps telling me 'loseText' is not a member of 'UnityEngine.GUIText'. Please help, please and thank you! 0 Answers

Is this a problem of unity 3 or i am doing something wrong. 1 Answer

What Am I Doing Wrong? Variable Names 3 Answers


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