Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
This question was closed Apr 12, 2018 at 11:52 AM by BillCipher_A-O-T-U for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by BillCipher_A-O-T-U · Oct 17, 2017 at 05:12 PM · methods

Best way to create children once

So i'm creating a simple genetic algorithm game, and i have come to the point to implement the breeding of my creatures. The creatures should trigger the breeding when a WillingToBreed boolean is true and they come in contact with another creature. I only want the genetic algorithm to trigger once, and for both parents, to produce children. How can i make it so the two functions end up running one function?

Currently I have this:

     void OnCollisionEnter2D(Collision2D collision)
     {
         if (willingToBreed)
         {
             if (collision.otherCollider.gameObject.GetComponent<Creature>().willingToBreed)
             {
                 //Do the breeding
             }
         }
     }

This is in every creature, so when they touch the "//Do the breeding" will be called twice, however i want code do be run ONCE, per both creatures.

I tried making a different class that revives breeding requests, filters out the duplicates then breeds, however if i put the filtering code in the update method, im not sure in what order the updates get called, so if the update gets called in between the creature updates, it could breed twice. Not sure if thats how it works, so im asking how the best way do it.

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

  • Sort: 
avatar image
0
Best Answer

Answer by Lairinus · Oct 17, 2017 at 06:03 PM

So the problem is that when two Creatures touch each other, it's impossible to call something only once inside of the event because both events fire simultaneously. You need a Singleton class (BreedingSingleton) and you need to add it to a GameObject.

The following code adds the breeding creatures to the list, and then uses a Coroutine to wait every frame until the Creature count is greater or equal to 2 and calls the "HandleBreeding" method. BreedingSingleton.HandleBreeding() is where your code should go when two animals breed

This code will only allow animals to breed once. If you want it to do more, extend it yourself :)

 public class Creature: MonoBehaviour
 {
     public bool willingToBreed = true;
     void OnCollisionEnter2D(Collision2D collision)
     {
         if (willingToBreed)
         {
             if (collision.otherCollider.gameObject.GetComponent<Creature>().willingToBreed)
             {
                 BreedingSingleton.AddCreatureToBreedingList(this);
             }
         }
     }
 }
 
 public class BreedingSingleton : MonoBehaviour // Add this in your game to one object
 {
     public static List<Creature> currentBreedingCreatures = new List<Creature>(); // Animals that currently want to breed
     public static List<Creature> closedBreedingCreatures = new List<Creature>(); // Animals that can no longer breed
     IEnumerator Start()
     {
         // Dump old references in case this is used between scenes
         currentBreedingCreatures = new List<Creature>();
         closedBreedingCreatures = new List<Creature>();
 
         while (true)
         {
             yield return null;
 
             // Check every frame for animals that are waiting to breed. If there are at least 2, we continue.
             if (currentBreedingCreatures.Count >= 2)
             {
                 // Breed them.
                 HandleBreeding(currentBreedingCreatures[0], currentBreedingCreatures[1]);
             }
         }
     }
 
     // Put your code that handles the breeding inside of here. 
     private void HandleBreeding(Creature creature1, Creature creature2)
     {
         RemoveCreatureFromBreedingList(creature1);
         RemoveCreatureFromBreedingList(creature2);
 
         // Add whatever you need to finish breeding the creatures here. Whatever is here will only happen once.
     }
 
     public static void AddCreatureToBreedingList(Creature creature)
     {
         if (creature == null)
         {
             Debug.Log("Attempted to add a null creature into the currentBreedingCreatures list. Path: BreedingSingleton.AddCreatureToBreedingList(Creature creature)");
             return; // Creature is null. We HATE nulls.
         }
 
         if (closedBreedingCreatures.Contains(creature))
             return; // Creature already bred. We don't want this one.
 
         if (currentBreedingCreatures.Contains(creature))
             return; // We already added this creature. We don't want this one, either.
 
         currentBreedingCreatures.Add(creature);
     }
 
     private void RemoveCreatureFromBreedingList(Creature creature)
     {
         if (currentBreedingCreatures.Contains(creature))
             currentBreedingCreatures.Remove(creature);
 
         closedBreedingCreatures.Add(creature);
     }
 }
 
Comment
Add comment · Show 1 · 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 BillCipher_A-O-T-U · Oct 17, 2017 at 06:50 PM 0
Share

Thank you!

avatar image
0

Answer by Bunny83 · Oct 17, 2017 at 05:23 PM

Uhm when you have breed one creature, don't you want to reset the "willingToBreed" variables back to false?

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

Follow this Question

Answers Answers and Comments

72 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Simply calling a method from another script :( 1 Answer

Make method variable show up in Inspector 1 Answer

Optional parameter, editor raises error "Unexpected symbol `=' " 0 Answers

Generalized call of specific methods 1 Answer

Using a method output in an if statement. 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