Put Picture.png into Hierarchy, drag it back into Project to create a Prefab and leave it named as “Picture”
Attach new script to Prefab called “InstantiatePicture”
Now here is my code:
public class InstantiatePicture: MonoBehaviour {
public GameObject Picture;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if(Input.GetKey (KeyCode.N)){
Instantiate(Picture);
}
}
}
This seemingly simple code doesn’t work.
I want to Instantiate this object and let it fall due to gravity (as a Rigidbody?). You can probably tell I am a beginner so any tips would be appreciated! Thank you.
So, just to make sure: Do you still have your ‘Picture’ object in the scene view? If you don’t have the object which has the script attached to it in the scene view, the script does not get launched. Second, did you make sure you have dragged the object you want to instatiate onto the ‘Picture’ in the scene view? Third: Let me write a bit of code for you.
In this piece of code I use ‘Input.GetKeyDown’. If you use Input.GetKey, the code inside the ‘if’ statement gets fired as long as you keep the button pressed. GetKeyDown only gets fired once the button is pressed and you have to press the button again to launch it again.
I also used Resources.Load() which is a very neat helper. To use this you just have to make a new folder in you project view with the name ‘Resources’ (capital R). Then you have to drag your prefab into the Resources folder. After this is done, you can instantiate any prefab inside of the Resources folder by just calling: Resources.Load(“PrefabName”). In your case this might be handy. Hope this helps!