- Home /
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.
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.
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.
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.