Hi guys , I need help with a basic button that will light up( turn red, I want to make this game for andriod and IOS too, so can’t use lights, use up too much memory )
var i : int ;
var RedTime : float = 3.5 ;
function Update()
{
var theBars = gameObject.Find(playtochange).GetComponent(TeleportBack);
//if ((theBars.waitTime = RedTime)) ;
//myLight.intensity = 5;
theBars.waitTime= i ;
if (( i == RedTime)) ;
{
{
renderer.material.color += Color(10, 0, 0) * 1;
// Time.deltaTime
}
else
{
renderer.material.color += Color(0, 0, 0)// ideally change this to force the default color
}
}
Ohh yea, the condition is that I check is in one of my game objects .
Like if I want a button to light up when barsTime is equal to 3.2 then I’m trying to code,
If ( barsTime=RedTime)
{
turn gameObjectRed
}
Still need help with this, if anyone can help out …
worth noting that color uses floats, that are from 0 - 1
0 = 0
0.5 = 128~
1 = 255
I see a few problems with what you currently have here.
First, you are checking an int against a float. This will never happen, an int will never equal 3.5. if you need it to be 3.5 you’ll have to change int i to float i.
Second, do you really want to use += to set the color value? Like jlcnz said, colors use floats and adding them together will not change any of the values. Also, unless you need an uncommon color, it would be easier to just use Color.red or any of the colors that are standard in unity. have a look at this page to see what’s already possible. I think you want renderer.material = Color.red.
Third, you have more curly braces between the begining of the if and the end of the else statement. If it’s not directly causing problems then it’s at least making it harder to read.
Hope that helps solve your problem.
Is this what you are looking for Keith?
var redTime : float = 3.5;
var theBars : GameObject;
function Start()
{
theBars = gameObject.Find(playtochange).GetComponent(TeleportBack);
}
function Update()
{
if (theBars.WaitTime == redTime)
{
renderer.material.color = Color(1.0, 0.0, 0.0);
}
}
LordAelfric
Starleaf Labs
Thanks to everyone helping out !
I’ll try this stuff out after class.
This is what I ended up with
var redTime : float = 3.5;
//var IR : float = 1;
var theBars : GameObject;
var Bars : String ;
function Update()
//
//while(true)
{
// if (theBars.WaitTime == redTime)
var theBars = gameObject.Find(Bars).GetComponent(TeleportBack);
if ( redTime== theBars.waitTime )
{
renderer.material.color += Color(0.6, 0, 0);
}
else
{
renderer.material.color -= Color(0.3, 0, 0) ;
}
}
UPDATE- for some reason this crashes unity, so i’m going to leave it out for now
My first guess would be the += and -= on the assignment of the colors. You should just be using = to set the values, otherwise they will continue to add/subtract that color value. As a tip, you should put your Find statement in the Start() of the MonoBehaviour. The call to Find will be slow during the Update() and is best stored as a member of the class.
LordAelfric
Starleaf Labs
var redTime : float = 3.5;
//var IR : float = 1;
//var theBars : GameObject;
var Bars : String ;
function Update()
//
//while(true)
{
// if (theBars.WaitTime == redTime)
var theBars = GameObject.Find(Bars).GetComponent(TeleportBack);
if ( redTime== theBars.waitTime )
{
renderer.material.color = Color(0.6, 0, 0);
}
else
{
renderer.material.color = Color(0, 0, 0) ;
}
}
This is what works for me, but for it to feel like a button( as in avoiding a solid red look ) I put a transparent cube on top of it, Much thanks LordAelfric
Hmm the code works for all but one game object, for that one object the light turns on at the start of the game( as its set to its default value at the start of the game . ) But latter when the value of waitTime turns back to said value, the button refuses to turn on. Of course a very crude work arround would be to set the value of waitTime to a value that i never use in game . And write a seperate( actually about 10 different scripts) to a function start to change the waitTime value to what I need it to be at the start of the game .