Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Creating/Controlling multiple dynamic instances of a Script object.

Discussion in 'Scripting' started by Hyubusa, Jul 5, 2015.

  1. Hyubusa

    Hyubusa

    Joined:
    Jun 18, 2015
    Posts:
    5
    I am trying to make multiple instances of my "Fly" object so that I can control the position and movement of each one independently. I am new to C# and I think I am missing something critical in my understanding here. Any light shining would be VERY appreciated.

    I have my Fly.cs:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Fly : MonoBehaviour {
    5.     public float myX,myY,myZ;
    6.     public float targetX,targetY,targetZ;
    7.     public Transform myFly;
    8. }
    Then I have my FlySystem.cs where I am trying to control multiple instances of Fly.
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class FlySystem : MonoBehaviour {
    5.  
    6.     public int numberOfFlies = 5;
    7.     private Fly[] flyList;
    8.  
    9.     void Start(){
    10.         flyList = new Fly[numberOfFlies];
    11.         CreateFlies ();
    12.     }
    13.  
    14.     void CreateFlies(){
    15.         for (int i=0; i < numberOfFlies; i++) {
    16.             flyList[i] = GetComponent<Fly>();
    17.             Debug.Log (flyList[i]);
    18.         }
    19.     }
    20. }
    21.  
    Problem is, flyList is coming back Null, which means if I try to reference it obviously I get errors saying it isn't an object. I do not really understand where to go from here.
     
  2. steego

    steego

    Joined:
    Jul 15, 2010
    Posts:
    968
    The problem is this line
    Code (csharp):
    1. flyList[i] = GetComponent<Fly>();
    This method assumes there is a monobehaviour called Fly on the same object as your FlySystem, which you obviously don't have as it is returning null. It would also always just return the same Fly, so you would end up with an array where all the elements were the same Fly.

    Instead, you could just make your flyList public, and then just assign all the flies in the inspector. This way, you wouldn't need any of the code you have in your FlySystem class other than public Fly[] flyList;

    Another way to solve this, is to create the flies as children of your FlySystem object in the editor, and then use GetComponentsInChildren to get all the flies like so

    Code (csharp):
    1. public class FlySystem : MonoBehaviour {
    2.     private Fly[] flyList;
    3.     void Start() {
    4.         flyList = GetComponentsInChildren<Fly>();
    5.     }
    6. }
    Either way should work fine, but having the flies as children might make more sense.
     
  3. Hyubusa

    Hyubusa

    Joined:
    Jun 18, 2015
    Posts:
    5
    I see what you are saying, and that makes sense. I think my end goal may be a bit foggy here.

    Maybe if I show you my original code before I created Fly.cs it would make a bit more sense, you could also see my desired end results a little better.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class FlySystem : MonoBehaviour {
    5.     public Transform flyVis;
    6.     public int numberOfFlies = 5;
    7.  
    8.     private Transform obj;
    9.     private BoxCollider container;
    10.     private float maxX, maxY, maxZ;
    11.     private bool created = false;
    12.     private Transform[] flyList;
    13.  
    14.     void Start(){
    15.         obj = GetComponent<Transform> ();
    16.         container = obj.GetChild (0).GetComponent<BoxCollider>();
    17.         flyList = new Transform[numberOfFlies];
    18.  
    19.         maxX = container.bounds.extents.x;
    20.         maxY = container.bounds.extents.y;
    21.         maxZ = container.bounds.extents.z;
    22.  
    23.         CreateFlies ();
    24.     }
    25.  
    26.     void CreateFlies(){
    27.         Vector3 myPos;
    28.         float myX, myY, myZ;
    29.         Transform f;
    30.         for (int i=0; i < numberOfFlies; i++) {
    31.             myX = container.bounds.center.x - Random.Range(-maxX,maxX);
    32.             myY = container.bounds.center.y - Random.Range(-maxY,maxY);
    33.             myZ = container.bounds.center.z - Random.Range(-maxZ,maxZ);
    34.             myPos = new Vector3(myX,myY,myZ);
    35.             f = Instantiate(flyVis, myPos, Quaternion.identity) as Transform;
    36.             flyList[i] = f;
    37.         }
    38.         created = true;
    39.     }
    40.  
    41.     void FixedUpdate(){
    42.         if(created){
    43.             moveFlies();
    44.         }
    45.     }
    46.  
    47.     void moveFlies(){
    48.         for (int i=0; i < numberOfFlies; i++) {
    49.             Debug.Log(flyList[i]);
    50.             /*How to set each one on it's own random path?
    51.             this method will have all of them going to the same
    52.             random position all the time, which isn't what I want.
    53.             So I created Fly.cs to try and set a custom Paramater for
    54.             each Fly, "targetPos" that would be unique for each fly
    55.             */
    56.         }
    57.     }
    58. }
    59.  
     
  4. steego

    steego

    Joined:
    Jul 15, 2010
    Posts:
    968
    Well if you really want to create them from code, you can do it something like this

    Code (csharp):
    1.  
    2.     void CreateFlies(){
    3.         for (int i=0; i < numberOfFlies; i++) {
    4.             GameObject go = new GameObject("Fly " + i.ToString());
    5.             flyList[i] = go.AddComponent<Fly>();
    6.             Debug.Log (flyList[i]);
    7.         }
    8.     }
    9.  
     
    Hyubusa likes this.