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 Game_Creator22 · May 14, 2021 at 04:25 PM · listlistsdestroy object

Why is list[0] not working for me?

I'm trying to simulate evolution where u get eliminated if ur score is the lowest. When I use the function Destroy(List[]) from some for me unknown reason it only destroys the first and the fift element in the list. Please help me! here is my code:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using System.Linq;
 using System.Runtime.InteropServices;
 using System.Security.Cryptography.X509Certificates;
 
 public class Evolution : MonoBehaviour
 {
     public List<GameObject> Players;
     public float timer;
     public GameObject playerpre;
     private GameObject player;
     public float w1a, w2a, w3a, w4a, w5a, w6a, w7a, w8a, w9a, w10a, w11a, w12a, w13a, w14a, w15a, w16a;
     public float w1b, w2b, w3b, w4b, w5b, w6b, w7b, w8b;
     public float b11, b12, b13, b14, b21, b22;
     // Start is called before the first frame update
     void Start()
     {
         GetPlayerList();
     }
 
     // Update is called once per frame
     void Update()
     {
         GetPlayerList();
         Players = Players.OrderBy(Player => Player.GetComponent<AIMovement>().score).ToList();
         timer += Time.deltaTime;
         if (timer >= 15)
         {
             int i = 0;
             while (i < 5)
             {
                 Destroy(Players[0]);
                 Players.Clear();
                 Players.AddRange(GameObject.FindGameObjectsWithTag("Player"));
                 timer = 0f;
                 i++;
             }
             i = 0;
             /*while (i < 5)
             {
                 w1a = Players[0].GetComponent<AIMovement>().w1a;
                 w2a = Players[0].GetComponent<AIMovement>().w2a;
                 w3a = Players[0].GetComponent<AIMovement>().w3a;
                 w4a = Players[0].GetComponent<AIMovement>().w4a;
                 w5a = Players[0].GetComponent<AIMovement>().w5a;
                 w6a = Players[0].GetComponent<AIMovement>().w6a;
                 w7a = Players[0].GetComponent<AIMovement>().w7a;
                 w8a = Players[0].GetComponent<AIMovement>().w8a;
                 w9a = Players[0].GetComponent<AIMovement>().w9a;
                 w10a = Players[0].GetComponent<AIMovement>().w10a;
                 w11a = Players[0].GetComponent<AIMovement>().w11a;
                 w12a = Players[0].GetComponent<AIMovement>().w12a;
                 w13a = Players[0].GetComponent<AIMovement>().w13a;
                 w14a = Players[0].GetComponent<AIMovement>().w14a;
                 w15a = Players[0].GetComponent<AIMovement>().w15a;
                 w16a = Players[0].GetComponent<AIMovement>().w16a;
                 w1b = Players[0].GetComponent<AIMovement>().w1b;
                 w2b = Players[0].GetComponent<AIMovement>().w2b;
                 w3b = Players[0].GetComponent<AIMovement>().w3b;
                 w4b = Players[0].GetComponent<AIMovement>().w4b;
                 w5b = Players[0].GetComponent<AIMovement>().w5b;
                 w6b = Players[0].GetComponent<AIMovement>().w6b;
                 w7b = Players[0].GetComponent<AIMovement>().w7b;
                 w8b = Players[0].GetComponent<AIMovement>().w8b;
                 b11 = Players[0].GetComponent<AIMovement>().b11;
                 b12 = Players[0].GetComponent<AIMovement>().b12;
                 b13 = Players[0].GetComponent<AIMovement>().b13;
                 b14 = Players[0].GetComponent<AIMovement>().b14;
                 b21 = Players[0].GetComponent<AIMovement>().b21;
                 b22 = Players[0].GetComponent<AIMovement>().b22;
                 Destroy(Players[0]);
 
                 player = Instantiate(playerpre);
                 player.transform.position = new Vector3(0, -2.66f, 0);
 
                 player.GetComponent<AIMovement>().w1a = w1a;
                 player.GetComponent<AIMovement>().w2a = w2a;
                 player.GetComponent<AIMovement>().w3a = w3a;
                 player.GetComponent<AIMovement>().w4a = w4a;
                 player.GetComponent<AIMovement>().w5a = w5a;
                 player.GetComponent<AIMovement>().w6a = w6a;
                 player.GetComponent<AIMovement>().w7a = w7a;
                 player.GetComponent<AIMovement>().w8a = w8a;
                 player.GetComponent<AIMovement>().w9a = w9a;
                 player.GetComponent<AIMovement>().w10a = w10a;
                 player.GetComponent<AIMovement>().w11a = w11a;
                 player.GetComponent<AIMovement>().w12a = w12a;
                 player.GetComponent<AIMovement>().w13a = w13a;
                 player.GetComponent<AIMovement>().w14a = w14a;
                 player.GetComponent<AIMovement>().w15a = w15a;
                 player.GetComponent<AIMovement>().w16a = w16a;
                 player.GetComponent<AIMovement>().w1b = w1b;
                 player.GetComponent<AIMovement>().w2b = w2b;
                 player.GetComponent<AIMovement>().w3b = w3b;
                 player.GetComponent<AIMovement>().w4b = w4b;
                 player.GetComponent<AIMovement>().w5b = w5b;
                 player.GetComponent<AIMovement>().w6b = w6b;
                 player.GetComponent<AIMovement>().w7b = w7b;
                 player.GetComponent<AIMovement>().w8b = w8b;
                 player.GetComponent<AIMovement>().b11 = b11;
                 player.GetComponent<AIMovement>().b12 = b12;
                 player.GetComponent<AIMovement>().b13 = b13;
                 player.GetComponent<AIMovement>().b14 = b14;
                 player.GetComponent<AIMovement>().b21 = b21;
                 player.GetComponent<AIMovement>().b22 = b22;
 
                 player = Instantiate(playerpre);
                 player.transform.position = new Vector3(0, -2.66f, 0);
 
                 player.GetComponent<AIMovement>().w1a = w1a;
                 player.GetComponent<AIMovement>().w2a = w2a;
                 player.GetComponent<AIMovement>().w3a = w3a;
                 player.GetComponent<AIMovement>().w4a = w4a;
                 player.GetComponent<AIMovement>().w5a = w5a;
                 player.GetComponent<AIMovement>().w6a = w6a;
                 player.GetComponent<AIMovement>().w7a = w7a;
                 player.GetComponent<AIMovement>().w8a = w8a;
                 player.GetComponent<AIMovement>().w9a = w9a;
                 player.GetComponent<AIMovement>().w10a = w10a;
                 player.GetComponent<AIMovement>().w11a = w11a;
                 player.GetComponent<AIMovement>().w12a = w12a;
                 player.GetComponent<AIMovement>().w13a = w13a;
                 player.GetComponent<AIMovement>().w14a = w14a;
                 player.GetComponent<AIMovement>().w15a = w15a;
                 player.GetComponent<AIMovement>().w16a = w16a;
                 player.GetComponent<AIMovement>().w1b = w1b;
                 player.GetComponent<AIMovement>().w2b = w2b;
                 player.GetComponent<AIMovement>().w3b = w3b;
                 player.GetComponent<AIMovement>().w4b = w4b;
                 player.GetComponent<AIMovement>().w5b = w5b;
                 player.GetComponent<AIMovement>().w6b = w6b;
                 player.GetComponent<AIMovement>().w7b = w7b;
                 player.GetComponent<AIMovement>().w8b = w8b;
                 player.GetComponent<AIMovement>().b11 = b11;
                 player.GetComponent<AIMovement>().b12 = b12;
                 player.GetComponent<AIMovement>().b13 = b13;
                 player.GetComponent<AIMovement>().b14 = b14;
                 player.GetComponent<AIMovement>().b21 = b21;
                 player.GetComponent<AIMovement>().b22 = b22;
                 i++;
             }
             */
             timer = 0f;
         }
     }
 
     void GetPlayerList()
     {
         Players.Clear();
         Players.AddRange(GameObject.FindGameObjectsWithTag("Player"));
     }
 }

I'm trying to simulate evolution where u get eliminated if ur score is the lowest. When I use the function Destroy(List[]) from some for me unknown reason it only destroys the first and the fift element in the list. Please help me!

Comment
Add comment · Show 1
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 andrew-lukasik · May 14, 2021 at 04:28 PM 0
Share

Cache references to components. This:

 player.GetComponent<AIMovement>().w1a = w1a;
 player.GetComponent<AIMovement>().w2a = w2a;
 player.GetComponent<AIMovement>().w3a = w3a;
 player.GetComponent<AIMovement>().w4a = w4a;
 player.GetComponent<AIMovement>().w5a = w5a;

will look more readable and work faster as:

 var movement = player.GetComponent<AIMovement>();
 movement.w1a = w1a;
 movement.w2a = w2a;
 movement.w3a = w3a;
 movement.w4a = w4a;
 movement.w5a = w5a;

2 Replies

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

Answer by andrew-lukasik · May 14, 2021 at 05:57 PM

My best guess at what you're trying to do here:

Evolution.cs

 public class Evolution : MonoBehaviour
 {
     public GameObject prefab;
     const float k_tick_rate = 15f;
     void Start ()
     {
         InvokeRepeating( nameof(Tick) , time:0 , repeatRate:k_tick_rate );
     }
     void Tick ()
     {
         var players = new List<AIMovement>( GameObject.FindObjectsOfType<AIMovement>() );
         players.Sort( (lhs,rhs) => rhs.score.CompareTo(lhs.score) );
 
         // kill 5 worst:
         for( int n=0 ; n<5 && players.Count!=0 ; n++ )
         {
             int index = players.Count-1;
             var worst = players[index];
             players.RemoveAt(index);
             Destroy( worst );
         }
 
         // reproduce between 5 best:
         for( int i=0 ; i<4 && i<players.Count-1 ; i++ )
         {
             var parentA = players[i];
             var parentB = players[i+1];
 
             int numChildren = 3;
             for( int n=0 ; n<numChildren ; n++ )
             {
                 var child = Instantiate(
                     prefab ,
                     new Vector3( 0 , -2.66f , 0 ) ,
                     Quaternion.identity
                 ).GetComponent<AIMovement>();
 
                 child.wxa = parentA.wxa;
                 child.wxb = parentA.wxb;
                 child.bxx = parentB.bxx;
             }
         }
     }
 }

AIMovement.cs

 public class AIMovement : MonoBehaviour
 {
     public float score;
     public WXA wxa;
     public WXB wxb;
     public BXX bxx;
 }

GenesOrSomething.cs

 [System.Serializable] public struct WXA { public float w1a, w2a, w3a, w4a, w5a, w6a, w7a, w8a, w9a, w10a, w11a, w12a, w13a, w14a, w15a, w16a; }
 [System.Serializable] public struct WXB { public float w1b, w2b, w3b, w4b, w5b, w6b, w7b, w8b; }
 [System.Serializable] public struct BXX { public float b11, b12, b13, b14, b21, b22; }
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 Game_Creator22 · May 15, 2021 at 09:02 AM 0
Share

Yeah that's exactly it, but I don't understand the bit with Parent A and Parent B. What does it do??

avatar image andrew-lukasik Game_Creator22 · May 15, 2021 at 12:28 PM 0
Share

Reading your code I understood you're trying to implement some kind of duplication method. This code there is an example for something like an androgynous reproduction for high-scoring ones.

Code wasn't tested, so it's just an idea.

avatar image
0

Answer by Italyman · May 14, 2021 at 07:52 PM

you use the List "Players" but my first guess is that you need to create (don't know the right verb) the List with:

Players = new List();

Just put that in your "Start" method or your "GetPlayerList" method, if you really call it on every start.

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

164 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 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 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

A node in a childnode? 1 Answer

from list of entries, select and add to scene beneath previous 1 Answer

How to prevent a list from being cleared at runtime? 2 Answers

Remove and Destroy Object from Linked List C# 2 Answers

Hi, I'm trying to make a list based quest manager but I'm struggling to extract certain variables from the list. 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