- Home /
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!
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;
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; }
Yeah that's exactly it, but I don't understand the bit with Parent A and Parent B. What does it do??
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.
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.