- Home /
Re-spawn Object Help!
*Hello, Im doing a college project in which I have to send OSC messages to Pure Data, I need that object to re-spawn once it is destroyed by the collision with the ball (player)... Once the ball collides with the object, OSC is sending 1000s of messages to pure data instead of one. I want a single message to be sent when I hit the coin and then I want the coin to then, re-spawn. This is the code!
pragma strict
var coinEffect : Transform;
var oscRef : OSCTestSender2Jump;
var respawnTimer : float;
var collectedItem : float;
var delayTime : float;
var objectPrefab : Transform;
function OnTriggerEnter (info : Collider)
{ if (info.tag == "Player")
{
Debug.Log("Add coin counter here!");
Instantiate(coinEffect, transform.position, transform.rotation);
oscRef.coinCollide();
Destroy(gameObject);
if(collectedItem){
respawnTimer += Time.deltaTime;
if(respawnTimer > delayTime){
var newObject = Instantiate(objectPrefab, transform.position,
transform.rotation);
respawnTimer = 4.0;
}
}
}
}
Thanks!
Answer by alok-kr-029 · Jun 17, 2015 at 05:51 AM
why dont you use a bool as a flag eg . private bool flag = true; before collision just check if the flag is true once the collision is success turn the flag to be false ;
hope this will help
I am a complete noob in coding, could you please explain what you mean in more detail please?
Answer by ArkaneX · Jun 19, 2015 at 01:19 PM
Please check if the "Add coin counter..." message is logged once or multiple times.
If once, then problem probably lies inside coinCollide method. If multiple times, then it means that new instance of coin is immediately created, collide with player, send message, is marked as destroyed, spawn another coin, and the process continues. This can happen, if collectedItem condition is true and deltaTime
On the other hand, if deltaTime is greater than 4.0, instantiate code will probably never be called.
Anyway, the code won't work properly. I suggest that you don't destroy the object at all and then reinstantiate it, but just hide it for a given amount of time, and then show it again.
"Add coin counter" is logged only once! I am a complete noob in coding, so i do not know how to hide and unhide the object! Could you please explain?
the coin collide function in the OSC plugin is
public function coinCollide(){ osc$$anonymous$$ = Osc.StringToOsc$$anonymous$$essage("/melody"); oscHandler.Send(osc$$anonymous$$); }
Quick way to hide and unhide after 4 seconds:
void OnTriggerEnter(Collider info)
{
this.gameObject.SetActive(false);
Invoke("Enable", 4f);
}
private void Enable()
{
this.gameObject.SetActive(true);
}
Thank you so much, you don't know how much you have helped me here... Is there a way to make the respawn shorter than 4 seconds, if not, no problem, I can now do what I've tried to achieve for a very long time - thanks to you!
Sure - I put 4 as an example, but you can change it to anything you want. Second parameter in Invoke method is number of seconds. 'f' suffix denotes a float variable, so that you're not limited to integer numbers, and can use for example 2.5f
Your answer
Follow this Question
Related Questions
How do I send OSC messages from objects upon collision? 0 Answers
on collision, show/hide other model scripting ? 1 Answer
Cannot move FPSController after respawning at a spawn point. 1 Answer
Checking Object Collision Without Script 1 Answer
Can an object to transfer to another as it increases your speed? 1 Answer