Ok so imI'm trying to instantiate my CarMovement class with my population class for a Genetic Algorithm, but when i run the project, the car prefab doesnt spawn.
Everything is wired up in the inspector correctly. Anyone know whats going on? (You might notice that in my for loop in the Population class I sent the score variable of all instances to Debug.log. But when I run the project, only one instance of the score variable appears there)
Edit: The score variable is now appearing ten times as it should, but the car prefab is not spawning
HeresHere's the population class:
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Population : MonoBehaviour {
using System.Collections; using System.Collections.Generic; using UnityEngine; public class Population : MonoBehaviour { public int populationSize = 10; private CarMovement[] cars; // Use this for initialization void Start () { cars = new CarMovement[populationSize]; for (int i = 0; i < populationSize; i++) { cars [i] = new CarMovement (); cars [i].score = 1+i; Debug.Log (cars [i].score); } //cars[0].Start(); } // Update is called once per frame void Update () { } } }
HeresHere's the CarMovement/Individual class:
public class CarMovement : MonoBehaviour {
public class CarMovement : MonoBehaviour { public float speed = 2f; public Rigidbody2D rb2d; public Vector2 spawnPosition = new Vector2((1.0f)*0.1f,3.83f); public int score; public GameObject carPrefab; // Use this for initialization public void Start () { rb2d = GetComponent<Rigidbody2D> (); GameObject.Instantiate (carPrefab,spawnPosition,Quaternion.identity); Debug.Log ("YAAAA"); } // Update is called once per frame public void Update() { rb2d.transform.Translate (speed * Time.deltaTime, 0, 0); } } }
Thanks for your help!