2
\$\begingroup\$

In order to make the variable accessible from other classes, I have to make it static.

When I make it static, I can't edit it from the Unity's Inspector panel.

No way to be able to do both: accessible and editable from the panel?


EDIT:

This is the code so far (by following Savlon's idea)

BouncingBall.js

#pragma strict

public static BouncingBall instance;
public var velocityX : float = 1;
public var velocityZ : float = -1;


function Awake(){
    instance = this;
}

function Update () {

    transform.Translate(new Vector3(velocityX, 0, velocityZ) * Time.deltaTime);

    if (this.transform.position.x > 4.5){
        velocityX = -velocityX;
        this.transform.position.x = 4.49;
    }
    if (this.transform.position.x < -4.5){
        velocityX = -velocityX;
        this.transform.position.x = -4.49;
    }
    if (this.transform.position.z > 4.5){
        velocityZ = -velocityZ;
        this.transform.position.z = 4.49;
    }
    if (this.transform.position.z < -4.5){
        velocityZ = -velocityZ;
        this.transform.position.z= -4.49;
    }
}

function OnCollisionEnter(col : Collision){
    if(col.gameObject.name == "PlayerPad"){
        velocityZ = -velocityZ;
    }
}

brick.js (where I call the variable from)

#pragma strict

function OnCollisionEnter(col : Collision){

    if(col.gameObject.name == "Ball"){
        BouncingBall.instance.velocityZ = -BouncingBall.instance.velocityZ;
        Destroy(this.gameObject);
    }
}
\$\endgroup\$
1
\$\begingroup\$

Yeh you can't create a static variable and have it accessible from the inspector panel that I know of. However, you could try this

In your class that has the editable variable you could create a static instance variable of your script and have the editable variable as public so you can edit and access it like so.

public class StaticVariable : MonoBehaviour 
{

public static StaticVariable instance; //Static instance variable of your script
public int editableVariable;           //Variable you want to edit in the inspector

void Awake ()
{
    instance = this;               //Set this script as the instance variable
}

// Other class methods here...
}

To edit that variable from another class you would write:

StaticVariable.instance.editableVariable = 1;

or to access it you would write:

int variable = StaticVariable.instance.editableVariable;

That way you can still edit it in the inspector and access it from another class.

Not sure if this is what you are looking for or whether you would be able to use it... if not just let me know and I'll delete this answer as it is not helpful :)

Note - This method is written for C# NOT Javascript.

\$\endgroup\$
4
  • \$\begingroup\$ It is helpful: it's a good idea. However, since I'm working with javascript (which doesn't need to write the class, AFAIK the filename also stands for the name of the class that wraps all the code within), I've tried to do it in a JS way, but marks an error when I put public static BouncingBall instance; within BouncingBall.js file. \$\endgroup\$
    – Fabián
    Apr 21 '14 at 2:40
  • \$\begingroup\$ The error says "Unexpected token: BouncingBall" \$\endgroup\$
    – Fabián
    Apr 21 '14 at 2:42
  • \$\begingroup\$ @Fabián I am not very familiar with Javascript but I did find examples here unity3d.com/learn/tutorials/modules/intermediate/scripting/… \$\endgroup\$
    – Savlon
    Apr 21 '14 at 2:48
  • \$\begingroup\$ I am not a fan of Unity's Javascript hence why I use C#. I've searched around on the net but could not figure out how to reference a class in Unity Javascript. Well I did find out how, but when I attempted it myself, I kept getting errors. \$\endgroup\$
    – Savlon
    Apr 21 '14 at 3:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.