- Home /
activate GameObject dont work
hi, i have problem with activate gameobject, i try use GameObject.active = true;, GameObject.SetActive(true); and nothing work but when i write it with false it work, why is it? here is code what i use:
#pragma strict
static var speed = 0;
var particle : GameObject;
function Start () {
}
function Update () {
particleSystem.startSpeed = speed /20;
if(speed == 0) {
particle.SetActive(false);
}
else {
particle.SetActive(true);
}
}
do you get any errors? if so, what are they?
have you set particle
to contain a reference to the GameObject
you're trying to affect?
btw, you can simplify your code:
particle.SetActive((speed != 0));
ins$$anonymous$$d of the '`if`'
Any chance that the game object assigned to 'particle' is the same game object this script is attached to? If so, you should note that deactivating a game object results in the scripts on that object no longer getting Update() calls.
Also note that the compiler is going to infer that speed is an int, so if speed has any value below 20, it will evaluate to 0.
i dont know now what you think, it dont get eny errors, this particle object is empty gameobject with component particlesistem and this script and this gameobject is inserted to player... i try this:
pragma strict
static var speed = 0;
var particle : GameObject;
function Start () {
}
function Update () {
particleSystem.startSpeed = speed /20;
particle.SetActive((speed != 0));
}
i manually set deactivate this object and when i play game it do nothink. and yes speed can be 1500.
did you assign anything to the '`particle`' variable in the inspector?
Thx for help all it work how Robertbu say... im idiot i dont see i disable gameobject with script then this script cant work and next when i disable gameobject game dont see it than i work it litle diferent:
#pragma strict
static var speed = 0;
function Start () {
}
function Update () {
particleSystem.startSpeed = speed /20;
if(speed == 0) {
GetComponent(ParticleSystemRenderer).enabled = false;
}
else {
GetComponent(ParticleSystemRenderer).enabled = true;
}
}
Answer by revapps · Jul 20, 2014 at 03:27 PM
Remove the "else" statement. Instead, use "if".
#pragma strict
static var speed = 0;
var particle : GameObject;
function Start () {
}
function Update () {
particleSystem.startSpeed = speed /20;
if(speed == 0) {
particle.SetActive(false);
}
if (speed > 0){
particle.SetActive(true);
}
}
nothink, this i try too. when i play game it automatic set to false but when i move it do nothink only i can manually set true but it dont work automatic.