x


Insantiate once only

Simply, how do you insantiate an object once when using a function OnCollisionEnter? So in this script, how would you only make it instantiate one firework? (By the way, the rest of the script works - I'm not missing the function or the variables, this is just the relevent part).

if (hit.gameObject.name == "Bottom")    {


         var instance : GameObject = Instantiate(fireworks, Vector3(2159.701, -178, 3312) , transform.rotation);
         yield WaitForSeconds (5);
    }
} 

Thanks!.

more ▼

asked Apr 06 '11 at 02:23 PM

Muzz 1 gravatar image

Muzz 1
548 52 59 71

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

It would help to see the rest of your script, but, if you only want it to work ONCE and never again, you could do this:

var activated : boolean = false;

        if (hit.gameObject.name == "Bottom" && activated == false) {
             var instance : GameObject = Instantiate(fireworks, Vector3(2159.701, -178, 3312) , transform.rotation);
                 activated = true;
             }
         }

If you want it to work every few seconds, you could do this:

var activated : boolean = false;
var timer : float = 3.0;

        if (hit.gameObject.name == "Bottom" && activated == false) {
             var instance : GameObject = Instantiate(fireworks, Vector3(2159.701, -178, 3312) , transform.rotation);
                 activated = true;

                 //As Long As This Is Not Update

                 yield.WaitForSeconds(timer);
                 activated = false;
             }
         }

If it is update, use this:

var activated : boolean = false;
var timer : float = 3.0;

        if (hit.gameObject.name == "Bottom" && activated == false) {
             var instance : GameObject = Instantiate(fireworks, Vector3(2159.701, -178, 3312) , transform.rotation);
                 activated = true;

                 //If its not Update

                 TimerRun();
             }
         }

function TimerRun () {
    yield.WaitForSeconds(timer);
    activated = false;
}

Just so you know, I haven't tried any of these codes... If you get any errors, let me know!

-- - Hope I Helped!

more ▼

answered Apr 06 '11 at 04:16 PM

AVividLight gravatar image

AVividLight
1.9k 68 99 128

Can I at least get a +1 for effort? ( :

Apr 06 '11 at 04:21 PM AVividLight

I'm trying to get to 1K...

Apr 06 '11 at 04:22 PM AVividLight

It's unseemly to beg for points. :-) p.s. +10

Apr 06 '11 at 06:03 PM Bampf

LoL... I know, but 1K is so much cooler than 995...

Apr 06 '11 at 06:18 PM AVividLight

This is going to be annoying, but I sorted it myself. Just switched round the order of everything - I had my {} wrong. Anyway, I'll give you a tick because you tried hard! and I may end up using one of your examples.

Apr 06 '11 at 07:21 PM Muzz 1
(comments are locked)
10|3000 characters needed characters left
function OnCollisionEnter(col : Collision) {
    if (col.collider.CompareTag("Bottom")) {
        var instance = Instantiate(fireworks, Vector3(2159.701, -178, 3312) , transform.rotation);
    }
}

Please note that the above code is untested and may contain errors.

more ▼

answered Apr 06 '11 at 02:27 PM

e.bonneville gravatar image

e.bonneville
5.7k 100 116 165

Doesn't work, sorry.

Apr 06 '11 at 03:56 PM Muzz 1

Is it giving you any errors? Make sure you have a collider tagged Bottom and that collision is actually occurring. You also need to have a variable named fireworks that contains a reference to a gameObject or prefab in order for this to work.

Apr 06 '11 at 04:15 PM e.bonneville
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x5069
x2486
x1670
x108

asked: Apr 06 '11 at 02:23 PM

Seen: 902 times

Last Updated: Apr 06 '11 at 07:42 PM