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

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

How to make trigger removes script. 1 Answer

Solved: Destroy command not working 1 Answer

OnTriggerEnter - destroy (this.gameobject) if it collides with anything 2 Answers

i want to destroy a projectile after it passes a limit on x axis after instantiation(2d game) 1 Answer

Instantiate prefab, add to list, destroy older prefab 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges