- Home /
trouble with instantiate
I'm trying to make a game that you can spawn objects with simply pressing the number keys.
and it's working great so far, but for some reason I can't spawn the third object
can someone help me?
here's the code:
using UnityEngine;
using System.Collections;
public class spawnscript : MonoBehaviour {
public GameObject cubeprefab;
public GameObject rectangleprefab;
public GameObject stairprefab;
public GameObject spawncube;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if(Input.GetKeyDown("1"))
{
if(idea.Instance.shadow1 == true)
{
//Vector3 position = new Vector3(camera.transform.position.x + 2, camera.transform.position.y);
Instantiate(cubeprefab, transform.position, transform.rotation);
//Physics.IgnoreCollision(cubeprefab.collider, spawncube.collider);
}
}
if(Input.GetKeyDown("2"))
{
if(idea.Instance.shadow2 == true)
{
Instantiate(rectangleprefab, transform.position, transform.rotation);
}
if(Input.GetKeyDown("3"))
{
//if(idea.Instance.shadow3 == true)
//{
Instantiate(stairprefab, transform.position, transform.rotation);
//}
}
}
}
}
well no, there are no error messages. the stairprefab just won't spawn into the scene =S
Insert a Debug.Log("$$anonymous$$ey 3 pressed") into if(Input.Get$$anonymous$$eyDown("3"))
try swapping Instantiate(stairprefab, transform.position, transform.rotation); with Instantiate(rectangleprefab, transform.position, transform.rotation); just to see if its the get key down or if its the instantiated object that is the problem. eg if u swap them and pressing 2 makes the stair prefab work, the getkeydown is stuffing up, but if u press 3 and the rectangle works its the stair prefab
Answer by Uniquesone · Oct 18, 2011 at 08:40 AM
You forgot an } after the instantiate of key input 2!
Remember to remove an } at the end of the file, or you would get an error =)
...it works now. Oh god, how clumsy of me x'D
but thanks man! : D
Your answer
Follow this Question
Related Questions
Instantiating prefab of object like it was in the scene 0 Answers
Instantiating prefabs: "The object of type GameObject has been destroyed". 1 Answer
How can I determine an object of prefab's size (for positioning when instantiating them) 2 Answers
Instantiate prefab once help C# 1 Answer
Missing Material in AssetBundle Prefab 3 Answers