x


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.

more ▼

asked Dec 03 '11 at 02:26 AM

Karsnen gravatar image

Karsnen
753 28 47 56

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

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.

more ▼

answered Dec 03 '11 at 02:38 AM

jahroy gravatar image

jahroy
3.2k 14 18 42

It worked out pretty well. My 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.

Dec 03 '11 at 10:50 PM Karsnen

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.

Dec 03 '11 at 10:56 PM Karsnen
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x5270
x3811
x3568
x356
x150

asked: Dec 03 '11 at 02:26 AM

Seen: 953 times

Last Updated: Dec 03 '11 at 10:56 PM