How to dynamically create a Prefab using Run Time

I am trying to have a very simple prefab be created at run time, it is just a Plane’s primitive with a texture attached to it and as far as i can tell it should working, but it does not show up where it should.

Here is the script that I am using, it is attached a large Plan primitive, just created with the Game-object Menu.

using UnityEngine;
using System.Collections;

public class Spawn_squares : MonoBehaviour {

public GameObject mod;


// Use this for initialization
void Start () 

{
	
for(int i = 0;i < 10;i++)
	{
	 	for(int j = 0;i<10;j++)
		{
//	mod = GameObject.Find("PlaneS");
			mod = (GameObject)Instantiate(GameObject.Find("PlaneS"),
				transform.position,transform.rotation);
			Debug.Log("HEllo"+i);
		}
		
		
	}
	
	
}

// Update is called once per frame
void Update () {
if(Input.GetKey(KeyCode.A))
	{
		
	transform.Translate(new Vector3(0,-1,0));	
		
		
	}
}

}

Well, you could at least give me a link, and also tell me why it freezes unity when i hit play.

Is it just taking a long time to make the planes or are you just not seeing the planes? Look in the hierarchy view to see if the planes are getting created. If they are getting created then: [http://answers.unity3d.com/questions/412718/plane-not-showing-up-in-game-view.html][1] [1]: http://answers.unity3d.com/questions/412718/plane-not-showing-up-in-game-view.html

1 Answer

1

Dont exactly know what you are trying to achive. But here is a way to create a primitive plane and then instantiate it.

	GameObject obj;
	void Start () 
	{	
		
		obj = GameObject.CreatePrimitive(PrimitiveType.Plane);
		Instantiate(obj);	
	}

You can also instantiate from a prefab in the following way. It is a basic and simple way to do it in.

[SerializeField]
GameObject obj;
void Start () 
{
	Instantiate(obj);
}

Then you pick which prefab you want to use in the inspector.
You can change the position of the object directly when you instantiate it.

Instantiate(obj,Position,Rotation);

Hope this help. Reply if not