- Home /
Desactive a object
Hi guys,
I doing a animated background. Basically when the object reaches the position.x is desactived.
But this sprite that i'm working now, don't move. So i need that the animation occur one time, then the objective is desactived.
I'm using the code below for now...
Obs: Tamanho = SCALE
Thanks!
var speed : float;
var bloco : GameObject;
var maxposition : float;
var maxScale: int;
var minScale: int;
var tamanho: float;
function Start () {
}
function Update () {
tamanho = Random.Range(maxScale, minScale);
transform.localScale = new Vector3 (tamanho, tamanho,0);
transform.position += new Vector3(speed, 0, 0) * Time.deltaTime;
if (transform.position.x > maxposition)
{
bloco.SetActive(false);
}
}
I don't understand what you're trying to do or what the problem is. I believe you want your bloco gameObject to get deactivated when it passes certain coordinate on the x axis, right? If that's the case, whats the problem? Because your code seems fine, despite the fact that transform.localscale moves the object or whatever, the if you stated should work fine.
Yes it's fine...
But this is just a example that i use in object that really moves.
Now I have a animation that don't move, and I need that he plays once and desactive.
Answer by Ryujose · Oct 20, 2014 at 09:48 PM
You need to declare the float number "X" maxposition to deactivate GameObject named bloco.
You can declared it on function Start() and set the float number.
You need to declare float and int of speed, maxScale, minScale and tamaño.
Hope it will helpfully to you.
Regards.
Actually this code is already used for objects that moves on the screen.
But this object don't move. I need to know can I desactive him, after the animation play just once.
Try to declare a float number for "maxposition" because you're telling that if your object moves on position X when reach the max position GameObject will be deactivated.
If you don't move it then this script don't Works on it. Because you can't move 0X to 0X.
You should try to implement those methods and change the script for this animation.
Yeah, and this is what i'm doing for others sprites...
That was just a example.
I'm trying to use the Animation, but, How I can desactive the object when he ends the animation?
There are 2 ways that I know.
1) When the animation stop, deactivate the GameObject similar like the process of the script you post here.
2) When the animation stop, make "renderer.enabled = false" this makes the GameObject invisible. Check this documentation.
http://docs.unity3d.com/ScriptReference/Renderer-enabled.html
I think this should Works.
Your answer
![](https://koobas.hobune.stream/wayback/20220613165528im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Current speed of an object? 5 Answers
Animation play 1 Answer
Why isn't my animation being played? 0 Answers