I’ve been looking around Unity Answers for a while and there’s a lot of similar questions as this one, but they don’t achieve the result I’m looking for.
here’s the problem:
public Transform fire;
public GameObject clone;
void Update ()
{
if(Input.GetKeyDown (KeyCode.F))
{
clone = Instantiate(fire,transform.position,transform.rotation) as GameObject;
}
}
when I press ‘F’ in game though, the gameobject ‘clone’ still shows ‘None’ in the inspector. How do I make the instantiated prefab go there?
One way, make both object of the same type.
using UnityEngine;
using System.Collections;
public class Example : MonoBehaviour
{
public GameObject fire;
public GameObject clone;
void Update ()
{
if(Input.GetKeyDown (KeyCode.F))
{
clone = Instantiate(fire,transform.position,transform.rotation) as GameObject;
}
}
}
Or you can Instantiate the ‘fire’ Transform’s gameObject component.
using UnityEngine;
using System.Collections;
public class Example : MonoBehaviour
{
public Transform fire;
public GameObject clone;
void Update ()
{
if(Input.GetKeyDown (KeyCode.F))
{
clone = Instantiate(fire.gameObject, transform.position, transform.rotation) as GameObject;
}
}
}