- Home /
Question by
Conspiracy · Oct 22, 2017 at 05:31 AM ·
instantiate prefab
why is it slow to instantiate
so, i'm trying to instantiate an object but it so slow to instantiate, i need to wait 1 second after pressing one of the keyCode before i can instantiate
here's the codes: this is for the gameManager
public class spawn : MonoBehaviour {
public int tipe;
public int objID;
public Sprite spr;
public Sprite battery;
public Sprite adaptor;
public KeyCode r;
public KeyCode e;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (Input.GetKeyDown (r)) {
tipe = 1;
objID = 1;
spr = battery;
}
if (Input.GetKeyDown (e)) {
tipe = 2;
objID = 2;
spr = adaptor;
}
}
}
this one is for the object that's going to instantiate
public class preview : MonoBehaviour {
public int tipe2;
public bool start = false;
SpriteRenderer sp;
Color g = Color.green;
GameObject gm;
// Use this for initialization
void Start () {
sp = gameObject.GetComponent<SpriteRenderer> ();
gm = GameObject.Find ("gameManager");
}
// Update is called once per frame
void Update () {
if (tipe2 == gm.GetComponent<spawn> ().tipe) {
start = true;
sp.sprite = gm.GetComponent<spawn> ().spr;
} else {
sp.sprite = null;
start = false;
}
sp.color = g;
g.a = 0.25f;
}
}
and this last one is also for the instantiator
public class create : MonoBehaviour {
public GameObject bat;
public GameObject adpt;
GameObject a;
void OnMouseDown () {
if (gameObject.GetComponent<preview> ().start) {
if (GameObject.Find ("gameManager").GetComponent<spawn> ().objID == 1)
a = Instantiate (bat, transform.position, transform.rotation) as GameObject;
if (GameObject.Find ("gameManager").GetComponent<spawn> ().objID == 2)
a = Instantiate (adpt, transform.position, transform.rotation) as GameObject;
GameObject.Find ("gameManager").GetComponent<spawn> ().objID = 0;
GameObject.Find ("gameManager").GetComponent<spawn> ().tipe = 0;
}
}
}
Comment