0
votes

I really need your help to make my GUI.Button start a JS script (or C#) attached to a GameObject. Here is what I have in my application. A JS script "doRotate.js", that does a rotation on a GameObject.

The rotation starts if the value of "public var doRotation = false" is chanced to "true" by click in the Inspector

    #pragma strict

    public var doRotation = false;

    function Update()
    {
        if (doRotation)
        {
            transform.Rotate(new Vector3(0, 50, 0) * Time.deltaTime);
        }
    }

I also have a JS script that renders some GUI.buttons. I want button 2, once pressed to call (run) the "doRotate.js" script, meaning accessing the boolean "doRotation" and chance its value to "true", as if it is done in the Inspector.

Following is what I've tried so far, but I get this error "Error BCE0020: An instance of type 'doRotate' is required to access non static member 'doRotation'. (BCE0020)". Here is the code on the GUI.button:

    var native_width :  float = 480;
    var native_height : float = 320;
    var addImage : Texture2D;

    var btnTexture1 : Texture;
    var btnTexture2 : Texture;


    function OnGUI ()
    {
    var rx : float = Screen.width / native_width;
    var ry : float = Screen.height / native_height;
    GUI.matrix = Matrix4x4.TRS (Vector3(0, 0, 0), Quaternion.identity, Vector3 (rx, ry, 1));

    GUI.Box( Rect(20, 200, 429, 129) ,addImage, "");
      if (!btnTexture1) {
            Debug.LogError("Please assign a texture on the inspector");
            return;
            }

            GUI.Button(Rect(54, 222, 52, 35), btnTexture1);

    if (!btnTexture2) {
            Debug.LogError("Please assign a texture on the inspector");
            return;
            }

            if(GUI.Button(Rect(118, 222, 52, 35), btnTexture2));

                var runScript : GameObject[] =
                GameObject.FindGameObjectsWithTag("markerObject");
                for(var doRotation : GameObject in runScript) {
                var script : doRotate = doRotation.GetComponent(doRotate);
                if(script)
                doRotate.doRotation(); //Error BCE0020: An instance of type 'doRotate' is required to access non static member 'doRotation'. (BCE0020)
            }

What have I done wrong? I've trying for days,to make it work without success. How can I access this variable on the click of the GUI.Button?

Can someone, please help me out?

1

1 Answers

1
votes

This error occurs because you are trying to access a nonstatic member of "doRotate" with a class, not with an instance of it.

In this following code:

var script : doRotate = doRotation.GetComponent(doRotate);
if(script)
   doRotate.doRotation(); //Error BCE0020: An instance of type 'doRotais    required to access non static member 'doRotation'. (BCE0020)

You are setting the instance of doRotate to var script, this var stores a reference of doRotate instance, with this you can access the var "doRotation" and change his value.

Something like this:

var script : doRotate = doRotation.GetComponent(doRotate);
if(script)
   script.doRotation = true;