- Home /
Duplicate a code sheet for each game object
Hi everyone, in my game i want the layer to climb the stairs.Each step can hold him for seconds before collapse . The code worked just fine with one step but when i ayyach it to multiple steps and the player collide with the first one all the steps collapse at the same time.
**//the time that one step can hold the player
var stillTime=2.0;
var hitEnter:float;
function OnCollisionEnter()
{
hitEnter=Time.time;
}
function Update()
{
if(Time.time>(hitEnter+stillTime))
{
Destroy(gameObject);
print("i can't support you any longer :p");
}
}**
Do i have to attach a different code sheet to every step ??
From the code you have here you'll need to attach the script to each step.
The problem is not in attaching the code to each step but in duplicating the code sheet for each step step1=> code1 step2=> code 2 ...
Answer by Mai hime · Apr 23, 2011 at 01:21 PM
The code work fine when i use triggers
function OnTriggerEnter() { yield WaitForSeconds(2); Destroy(gameObject);
}
i attached two collider to each step one as trigger and the other for colliding thank you for helping me
Answer by Bunny83 · Apr 23, 2011 at 01:00 PM
Your problem is that if you don't collide with the platform your hitEnter time will be "0". Therefore all platforms will collapse after two seconds.
Do something like:
var stillTime = 2.0; var hitEnter = 0.0; function OnCollisionEnter() { if (hitEnter == 0.0) { hitEnter = Time.time; } }
function Update() { if((hitEnter > 0.0) && (Time.time>(hitEnter+stillTime))) { Destroy(gameObject); print("i can't support you any longer :p"); } }
That will only allow a platform to collapse when you have set a time. It also prevents the "retriggering" of the time when touching the platform again.
Your answer
Follow this Question
Related Questions
using Contains(gameObject) to find and destroy a gameObject from a list 2 Answers
Destroy Object when scene loads 1 Answer
object does not destroy 1 Answer
How to destroy a transform's parent object. 1 Answer
How to delay lines of code. 2 Answers