- Home /
 
How do instantiate stuff as a child
I made this script and looked all over the internet and tried everything I saw but I always get a error even tho I know I set it up properly but yet I still get a error. Atm I am using this set up and getting this error. If you could answer in code and not a link I well like that a lot better.
 read this before you assume its just fixing a error. 
 error
 Assets/Scripts/SpawnShips.cs(33,44): error CS0266: Cannot implicitly convert type `UnityEngine.Object' to `UnityEngine.GameObject'. An explicit conversion exists (are you missing a cast?)
 
 here's the script 
 using UnityEngine;
 using System.Collections;
 
 public class SpawnShips : MonoBehaviour {
     public string Help = "How many ships, type of ship, time befor start (keep it .5s or 1s";
     public Transform[] spawns;
     public GameObject[] SpaceShips;
     public Vector3[] positionArray = new [] { new Vector3(0f,0f,0f), new Vector3(1f,5f,5f) };
     private int currentPos = 0;
     private float time = 0;
     
     
     void Start () 
     {
         InvokeRepeating("spawn", 0, 0.5f);
     }
     
     void spawn()
     {
         int ships = Mathf.RoundToInt(positionArray[currentPos].x);
         int TypeOfShip = Mathf.RoundToInt(positionArray[currentPos].y);
         int TimeBeforStat = Mathf.RoundToInt(positionArray[currentPos].z);
 
         
         time += 0.5f;
         
         if(time == TimeBeforStat)
         {
             for(int i = 0; i < ships; i++)
             {
                 GameObject h;
                 h = SpaceShips[TypeOfShip];
                 GameObject instantiated = Instantiate(h.transform);            //instantiate the object
                 instantiated.transform.parent = spawns[i].transform;//assuming this script is attatched to the desired parent
                 instantiated.transform.localPosition = Vector3.zero;
             }
         }    
     }
     
 }
 
              Please provide the error text and line number.
If Unity throws an error you did not set it up properly.
Answer by kurotatsu · Sep 16, 2014 at 03:14 AM
Try this:
 for(int i = 0; i < ships; i++)
             {
                 GameObject h = (Instantiate(SpaceShips[TypeOfShip],spawn[i].position,spawn[i].rotation) as GameObject);
                 h.transform.parent = spawn[i].transform;
             }
 
              No prob, been there(just grabbed a snippet from an inventory I'm writing, and modified it for ya.).
Answer by Cherno · Sep 16, 2014 at 02:55 AM
Change line 41 (wher you instantiate) to
  Transform instantiated = Instantiate(h.transform) as Transform ;  //instantiate the object
 
               or
  GameObject instantiated = Instantiate(h.transform) as GameObject;  //instantiate the object
 
              That got rid of the error but can you also fix it cus it is not making it a child of the "spawns[i]"
The line
instantiated.transform.parent = spawns[i].transform;
can be simplified. spawns already is of the type transform[], so you can just write
instantiated.transform.parent = spawns[i];
Now, as long as there is a transform in spawns at position i, the transform that is stored in instantiated will be made a child of this (spawns[i]) transform.
Also, if instatiated is of the type transform too, then the line can be made even shorter:
instantiated.parent = spawns[i];
ok I tryed that but its still spawning as a clone and getting this error 
yet 
Take a look at your for... loop.
  int ships = $$anonymous$$athf.RoundToInt(positionArray[currentPos].x);
 
                  Why is ships an int that gets defined by a rounded float? You use this variable to loop through an array so it may never have a value of more than the array's Length! If "ships" is 12 and spawns.Length is 11, you will keep getting the NullReference Error because you are trying to access a part of the array that is neither defined nor exists, but is completely outside it's boundaries!
thanks for the help, but it is being rounded so that it can be a int
Answer by Kiwasi · Sep 16, 2014 at 03:19 AM
The code to instantiate a child is pretty straight forward
 GameObject clone = (GameObject)Instantiate(prefab);
 clone.transform.parent = transform;
 
               I suspect you have other things going wrong in your convoluted code. What is the actual error you are getting? Include the exact text of the error message and the line number.
I would also strongly recommend rewriting this script from scratch. Its a bit of a mess to follow.
Using the x, y, z components of a Vector3 to store the number of ships, type of ship and start time is messy. Storing data as float and then rounding it to int is messy.
Suppose you decide you want to store a fourth piece of information with each wave? Your entire code structure is immediately broken. Your code will also break if the z component of the Vector3 is not a multiple of 0.5f.
It took me a dozen tries just to figure out what you were trying to do. Readable script is very important if you want to come back to it later, or if you want others to help you debug it. Trust me, you will always want to come back and make changes later.
Some ideas to tidy up the code:
Use a custom struct to record the wave data. That way you can modify wave data easily
 public struct Wave {
 
     public int noOfShips;
 
     // You could actually store the prefab here ins$$anonymous$$d of an int
     public int typeOfShip;
     public float startTime;
 }
 // With that struct in place you can call use
 int ships = wave.noOfShips;
 // Ins$$anonymous$$d of
 int ships = $$anonymous$$athf.RoundToInt(positionArray[currentPos].x);
 
                  Use input validation with custom properties to check you input for time is valid. Or use a coroutine ins$$anonymous$$d of invoke to call your waves at proper frequencies.
 IEnumerator spawn(){
     while (true){
         yield return new WaitForSeconds (wave.startTime)
         // Instantiate the wave
     }
 }
 
                  None of this code is perfect, but it should give you a glimpse of what can be done.
Answer by MileSplit · Sep 16, 2014 at 02:47 AM
Use yourObject.transform.parent = parent
could you put that in the script cus either im putting it in wrong or somthing els
if you look at the script I have the same thing as your saying set up
Your answer
 
             Follow this Question
Related Questions
Instantiated GameObject gets spawned as a child 2 Answers
Instantiating as a child error 1 Answer
Multiple Cars not working 1 Answer