- Home /
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 perfor$$anonymous$$g a comparison.
I think you mean to use the below to assign the value: CubeScript.RED = true;
Answer by el-santia93 · Dec 10, 2013 at 10:44 AM
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;
}
}
Answer by Bunny83 · Dec 10, 2013 at 02:20 PM
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 CA$$anonymous$$.js, BLUE CA$$anonymous$$.js))
and BLUE, RED Scripts that i want to make the boolean true;
This is a whole mess. It's still not really clear what those classes should do. Usually the class name should geve a hint what a class is good for.
However in your "blue" and "red" scripts you don't use the variable you declared at the top (cube) but the class itself (Cube).
You have to use:
cube.RED = true;
// or
cube.BLUE = true;
However your two scripts won't work at the same time. First "Start" is called just once when the game starts. When you change your "ONOFF" variable afterwards it won't do anything.
Next thing is you set the "ONOFF" variable to true before your check in Start, so the one that is initialized last will actually matter.
Can you explain what this is good for? Generally having two script with almost identical code is a good sign for a bad concept.
Your answer
![](https://koobas.hobune.stream/wayback/20220613123841im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Call a Function when Object Created 2 Answers
GameObejct.Find using string variable for name 1 Answer
How to call a function from a script component on another object 1 Answer
OnControllerColliderHit function not called? [C#] 1 Answer
How to use the same script on different objects at the same time? 1 Answer