Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by AreebM · Apr 13, 2014 at 09:12 AM · 2drigidbodyinstantiateprefabbeginner

How can I Instantiate a Sprite into my 2D Scene?

Here's my process:

Import New Asset>Picture.png

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Malfegor · Apr 13, 2014 at 10:06 AM

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.

 void Update()
 {
     if(Input.GetGetDown(KeyCode.N))
     {
         GameObject NewPicture = Instantiate(Resources.Load("Picture")) as GameObject;
     }
 }

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!

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image steveh2112 · Dec 08, 2016 at 09:12 AM 0
Share

i read that using the Resources folder is BAD so i just spent a lot of time removing code like this https://unity3d.com/learn/tutorials/topics/best-practices/resources-folder now i add it to the gameobject and set in the inspector

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

23 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Why is my Prefab Instantiating when the Scene is Loaded? 2 Answers

need help with instantiate'ing a Rigidbody 1 Answer

Movement of an prefab without rigidbody? 1 Answer

Infinity runner - best way to spawn board in runtime. 0 Answers

When flipping player, instantiated objects spawn on different spot. 2D shooting 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges