Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Muzz 1 · Apr 06, 2011 at 02:23 PM · collisioninstantiatejavascript-specific

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!.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by AVividLight · Apr 06, 2011 at 04:16 PM

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!

Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image AVividLight · Apr 06, 2011 at 04:21 PM 1
Share

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

avatar image AVividLight · Apr 06, 2011 at 04:22 PM 0
Share

I'm trying to get to 1$$anonymous$$...

avatar image Bampf · Apr 06, 2011 at 06:03 PM 0
Share

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

avatar image AVividLight · Apr 06, 2011 at 06:18 PM 0
Share

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

avatar image Muzz 1 · Apr 06, 2011 at 07:21 PM 0
Share

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.

Show more comments
avatar image
0

Answer by e-bonneville · Apr 06, 2011 at 02:27 PM

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Muzz 1 · Apr 06, 2011 at 03:56 PM 0
Share

Doesn't work, sorry.

avatar image e-bonneville · Apr 06, 2011 at 04:15 PM 0
Share

Is it giving you any errors? $$anonymous$$ake 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.

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

No one has followed this question yet.

Related Questions

Get rotation from hit object 1 Answer

Reload Level on Collision 4 Answers

OnCollisionEnter not being called, between a Rigidbody and a Box Collider 1 Answer

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

help with scripting combo points 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges