- Home /
Help With Add and Remove on a loop
Hi I'm pretty new to scripting, and have got pretty stuck. I'm basically trying to add and remove an object on a loop with a 4 second delay in between. Any help would be awsome. p.s Turning the render on and off wont work, as the collider is still there.
we would be happy to help you debug your code if you would post it. otherwise it's somewhat of an abstract question.
I'm pretty noob so i have just tried different scripts i have found, tried editing a few but no success
I'd recommend learning to write code from scratch before trying to build a game by editing existing code. Try Code Academy as it's a good resource to learn scripting.
Let me be frank, it's nearly impossible to create a game without knowing how to write and think about code. Until you know what you're doing, you're going to struggle really badly.
Perchik,
It really annoys us to see people jump into game development with zero program$$anonymous$$g knowledge. Right?
Answer by Josh707 · Feb 21, 2014 at 07:56 PM
Disabling the GameObject itself with SetActive will disable all the components on it, including the collider. You can use a float subtracted by Time.deltaTime per-frame in the Update function to behave like a timer.
public GameObject go;
float delay = 4.0f;
float timer = 4.0f;
void Update(){
timer -= Time.deltaTime;
if(timer <= 0f){
timer = delay;
go.SetActive(!go.activeSelf);
}
}
Note that you have to do this from a different object because all components will be disabled, it will not continue to execute.
i get this error
(6,7): error CS0116: A namespace can only contain types and namespace declarations
i tried to change it to javascript myself , and came out with this , i have no errors , but it dosnt seem to do anything
var go : GameObject;
var delay : float = 4.0;
var timer : float = 4.0;
function Update(){
timer -= Time.deltaTime;
if(timer <= 0){
timer = delay;
go.SetActive(go.activeSelf);
}
}
The reason it doesn't work is because 'activeSelf' returns the state of the object. If it is active it returns true and vice versa. The exclamation (!) is used to invert a boolean so you can set it to the opposite of it's state. No problem though!
Answer by Twitch1139 · Feb 21, 2014 at 09:50 PM
this ended up working for me , with desired result.
var delay : float = 8.0;
var timer : float = 8.0;
var thePrefab : GameObject;
function Update(){
timer -= Time.deltaTime;
if(timer <= 0){
timer = delay;
var instance : GameObject = Instantiate(thePrefab, transform.position, transform.rotation);
Destroy(gameObject.Find("Cube1(Clone)"), 4);
}
}
oh and hanks Josh707 wouldnt have got this without your code