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*.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.

4 Answers

4

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*"*

The line instantiated.transform.parent = spawns*.transform;* can be simplified. spawns already is of the type transform[], so you can just write instantiated.transform.parent = spawns*;* <em>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*) transform.*</em> <em>Also, if instatiated is of the type transform too, then the line can be made even shorter:</em> <em><em>instantiated.parent = spawns*;*</em></em>

ok I tryed that but its still spawning as a clone and getting this error ![alt text][1] yet ![alt text][2] [1]: http://piclair.com/data/bewps.jpg [2]: http://piclair.com/data/xnvsh.jpg

Take a look at your for... loop. int ships = Mathf.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

Try this:

for(int i = 0; i < ships; i++)
            {
                GameObject h = (Instantiate(SpaceShips[TypeOfShip],spawn_.position,spawn*.rotation) as GameObject);*_

_ h.transform.parent = spawn*.transform;
}*_

thanks man

No prob, been there(just grabbed a snippet from an inventory I'm writing, and modified it for ya.).

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.

thank you that worked

just one thing I dont see how its messy

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