Skip to main content
Code formatting, tagging, cleanup
Source Link
DMGregory
  • 140.8k
  • 23
  • 257
  • 401

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!

Ok so im 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

Heres the population class:

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 () { } 

}

Heres the CarMovement/Individual class:

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!

I'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?

Here's the population class:

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 () {  } } 

Here's the CarMovement/Individual class:

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);    }  } 
Source Link

Object array in unity not instantiating properly

Ok so im 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

Heres the population class:

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 () { } 

}

Heres the CarMovement/Individual class:

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!