Call boolean from another script

i want to call boolean and i cant …

these are the scripts

Cube Script:

public var RED : boolean = false;

and i want to make it true from this script

RED CAM Script:

public var CubeScript : MonoScript;

function Start ()
{
	CubeScript(RED == true);
}

so what can i do

When you put RED == true, it is performing a comparison. I think you mean to use the below to assign the value: CubeScript.RED = true;

–

2 Answers

2

how bout you do this in your cube script have a function that that sets the Boolean true like this

function SetTrue(){
    red=true;
}

then call this function from your other script like this

function Start(){
    GameObject.Find("Cube").SendMessage("SetTrue");//cube here being the gameobject that has the first script attached to it
}

its right but i am hiding other things so i have this function Update () { if (touch >= 4) { if (RED == true) { renderer.material.color = Color.red; } } } so can u correct it

–

i don't understand what you mean by correct it but here goes function Update () { if (touch >= 4&&RED==true) { renderer.material.color = Color.red; } }

–

First MonoScript is an editor class which can’t be used at runtime. Also it represents the script file asset and not an instance of the class that might be defined in the script.

Next thing is you don’t “call” variables. You can access variables. Specifically you can “set”/“write” a variable or “get”/“read” a variable. You can only call methods. This is where most of the confusion about your question comes from i guess.

Your redcam script might should look like this:

public var cube : CubeScript;
 
function Update ()
{
    if (touch >= 4)
    {
        if (cube.RED)  // Since it's a boolean a comparison to true (==true) is again true so it's pointless
        {
            renderer.material.color = Color.red;
        }
    }
}

Note: “CubeScript” is the name of your cube script and you have to drag the object which has this cube script onto the cube variable in the inspector of your redcam script.

However since we have no idea what’s the purpose of all this we can’t suggest better ways.

ok now look at that why when i build it it gives me an error i'll give you all the scripts i need to make the boolean true from other scripts ... look But i dont want to edit Cube.js i want to edit ((RED CAM.js, BLUE CAM.js)) [19169-cube.png|19169] and BLUE, RED Scripts that i want to make the boolean true; [19170-red+blue.png|19170]

–