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 Osama_Javed · May 30, 2014 at 07:56 AM · destroygameobjectoverlapping

How to stop multiple overlapping spawns at one specific position?

I am making a 2D football themed game and I have currently set up 3 spawn points for a gameobject, a redcard. Whenever the football hits the randomly spawned redcard, it contributes -3 to my score. However, based on the code i have, multiple redcards spawn at a single spawn point and they spawn on top of each other. so when the ball hits that spawn point, all the overlapping red cards activate and my score plummets. I only want ONE red card at a specific spawn point. And i also would like for the red card to disappear after a certain amount of time after its spawned. I am very new to game development so your help is really appreciated. Thanks in advance. Here is my code. Redcard is the prefab. All of this works so far. its just the rest i need help with.

 var timer : float = 0.0;
 var spawning : boolean = false;
 var prefab : Rigidbody;
 var spawn1 : Transform;
 var spawn2 : Transform;
 var spawn3 : Transform;
  
 function Update () {
 if(!spawning){
   timer += Time.deltaTime;
  }
 
 if(timer >= 2){
   Spawn();
  }
 }
  
 function Spawn(){
 spawning = true;
  timer = 0;
  
  var randomPick : int = Mathf.Abs(Random.Range(1,4));
  
 var location : Transform;
  
 if(randomPick == 1){
   location = spawn1;
   Debug.Log("Chose pos 1");
  }
  else if(randomPick == 2){
   location = spawn2;
   Debug.Log("Chose pos 2");
  }
  else if(randomPick == 3){
   location = spawn3;
   Debug.Log("Chose pos 3");
  }
  
  
 yield WaitForSeconds(1);
  
  spawning = false;
 }
Comment
Add comment · Show 3
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 Owen-Reynolds · May 30, 2014 at 12:29 PM 0
Share

But, it you get them to disappear after a while, won't that automatically solve spawning on top of each other? (Since you'll only have one at a time?) Plus, AddForce moves them, so won't that automatically make then not overlap?

$$anonymous$$ost people use overlapSphere to make sure the area is clear. But in your case, just remember if the last one was 1, 2 or 3, and don't roll that number for the next one.

avatar image Osama_Javed · May 30, 2014 at 01:15 PM 0
Share

Well, first of all, thank you for catching my addforce mistake. $$anonymous$$y objects werent moving so removing those lines of code didnt really alter my outcome.

As for having them disappear after a while, there is an issue with this. The game object used to create clones to spawn would disappear as well. I tried using some "estroy gameObject after x time" scripts but didnt have much success.

And its interesting what you said about not rolling the last number the next time. How can i dictate the code to do that? Even if a told it not to roll for the next one, i still need it to respawn there if the redcard gets destroyed. I changed the time it takes to spawn to 10 seconds which are helping my odds since a game session lasts a $$anonymous$$ute.

Thanks again for your comment. Im currently experimenting with limiting spawns with instantiate.

avatar image Owen-Reynolds · May 31, 2014 at 04:51 PM 0
Share

Destroying the "master" object by mistake is a whole different thing. The suggested way is to use prefabs (which you can still try to destroy by mistake.)

When I try to solve a lot of little problems at once, I can't tell which problem caused what -- easier to simplify and fix one things at a time.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by smoggach · May 30, 2014 at 04:00 PM

A good strategy for solving these kinds of problems is to break it up into smaller problems and dependencies. Let's look at what is static about your scene: - You have 3 spawn points. - Your red cards all act the same way. - Some logic is deciding whether it's time to spawn a card or not.

So let's find a good angle to look at this problem. Spawn points depend on the card it contains. Cards depend on some logic to decide whether they should spawn or not. At the highest level there is something deciding when to spawn a card. So let's start there. 1. Create an object that does nothing but decide when to spawn a card. 2. Create a red card object that does nothing but spawn at a given point, do it's thing, then die. (or react when the user triggers it). 3. Create a spawn point object that does nothing but instantiate a red card object if all the conditions are met to do so then destroy it when necessary.

Now you have everything you need. Unity makes it incredibly easy to solve problems this way.

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

22 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Checking if object intersects? 1 Answer

overlapping hear a song 1 Answer

OverlapCircleAll vs BoxCollider2D performance 0 Answers

overlapping UV on mobile 1 Answer

How to Destroy Terrains (destroy(this.gameobject) not working) 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