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 Confuseddd · Apr 05, 2011 at 01:17 PM · instantiateplayerdestroyontriggerenter

Instantiate spawns 2 instead of 1, help?

var newBoat : Transform;

var dockboat = true;

function OnTriggerEnter (){

 if(GameObject.FindWithTag("Player") && dockboat == true){
         Destroy(GameObject.FindWithTag("Player"));
         dockboat = false;
         StartCoroutine("spawn");
 }

}

function spawn () { Instantiate( newBoat, transform.position, transform.rotation); dockboat = false; StartCoroutine("trueagain", 1); }

function trueagain () { dockboat = true; }

I'm having trouble with this script. When I collide with the trigger I want to destroy my character and spawn a different one. Most of the time it spawns 2 characters in the same place at the same time, other times it just spawns one character. I have absolutely no idea why it is doing this. Is there an easy fix to this problem?

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
0
Best Answer

Answer by oliver-jones · Apr 05, 2011 at 01:38 PM

I believe its being caused by the 'StartCoroutines' that you have.

Try replacing them with a:

Invoke("spawn", 0);

and

Invoke("trueagain", 0);

Hint:

The '0' in the Invoke is if you would like a time delay to trigger the function, so if you have a: Invoke("spawn", 10); - then your spawn function will be called in 10 seconds.

Hope that helps.

Comment
Add comment · 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
0

Answer by Owen-Reynolds · Apr 06, 2011 at 08:14 PM

The basic problem seems to be that Instantiating the new boat is reTriggering the trigger box. In other words, new spawns can cause OnTriggerEnter. You've got the "flag" spawnBoat to prevent that.

The rest of the problem is likely that coroutines can run in any order. Sometimes it sets dockboat=false, spawns, resets it to true, then reruns OnTriggerEnter for the 2nd boat, with spawnBoat back to true.

Adding a Wait or yield command to TrueAgain would cause everything else to run first (with a false spawnBoat.) Likewise adding a delay to Invoke would do the same thing.

A comment: I don't think you could move the lines from spawn into OnTriggerEnter.

Comment
Add comment · 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

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

Resources.load loads twice ( onTriggerenter->load-another->destroy-self ) 1 Answer

Streaming Game Content 1 Answer

Solved: Destroy command not working 1 Answer

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

Timetrail script creating 2 trails and not destroying the second 0 Answers


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