• 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 Wittz8 · Jan 27, 2020 at 05:56 PM · scripting problemspritesinstantiation

Can't Instantiate

Beginner here with a noob question. I'm trying to instantiate a sprite (redBall), but I can't quite get my code to work. This is the part of the script that is relevant:

 public Sprite redBall;
 private void Start() {
             Instantiate(redBall, new Vector3(0.0f, 0.0f, 0.0f), Quaternion.identity);
         }

The Sprite field shows up in the inspecter, and I use the drag and drop method to set it. But when I run the program, nothing shows up. There's no compiler error, and when I manually place the sprite in the hierarchy, it does show up, so I know it's not a problem with the sprite object itself.

Thank you for your time.

Comment
Add comment · Show 2
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 Hellium · Jan 27, 2020 at 05:46 PM 0
Share
  1. Is the script attached to an active gameObject in the scene ?

  2. Is the Start function called? (Add a Debug.Log("Start called"); to find out)

  3. Have you provided the redBall sprite in the inspector of the component?

  4. Does something appear in the hierarchy after you entered play mode? (the sprite may be instantiated but not visible by the camera)

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Magso · Jan 27, 2020 at 05:57 PM

The first argument of instantiate needs to be a GameObject type, you're trying to instantiate a Sprite type.

Comment
Add comment · Show 7 · 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 Wittz8 · Jan 27, 2020 at 06:00 PM 0
Share

$$anonymous$$aybe I'm not seeing something -- I thought that's what I did. The first argument is "redBall," correct? And I declared it as a Sprite already, no?

avatar image Magso Wittz8 · Jan 27, 2020 at 06:10 PM 0
Share

S̶p̶r̶i̶t̶e̶ sorry SpriteRenderer is the component in which case AddComponent would be used. Instantiate adds an object to the scene along with it's components i.e. makes copies of prefabs in the scene.

For similar context.

 //Like how
 float myFloat = 5;
 //and
 String myString = "5";
 //can not be used the same way.

avatar image Hellium Wittz8 · Jan 27, 2020 at 06:13 PM 0
Share

$$anonymous$$agso is almost right, a Sprite is an asset. Instantiating a sprite creates a duplicate of this asset (not stored in your Project folder though).


What you want to do is to create a GameObject with a SpriteRenderer referencing your sprite.


So either create a prefab of the GameObject already setup, and instantiate this prefab, or create a gameObject from code, attach a SpriteRenderer to it and provide the sprite.

 public GameObject redBallPrefab;
 private void Start() {
     Instantiate(redBallPrefab, new Vector3(0.0f, 0.0f, 0.0f), Quaternion.identity);
 }


 public Sprite redBall;
 private void Start() {
     GameObject ballGameObject = new GameObject("redBall");
     SpriteRenderer spriteRenderer = ballGameObject.AddComponent<SpriteRenderer>();
     spriteRenderer.sprite = redBall;
 }
avatar image Wittz8 Hellium · Jan 27, 2020 at 06:28 PM 0
Share

I thought I was creating the prefab. I've created a sprite in my assets and used the drag and drop method to drag it into the inspector. I thought this is what creating and using a prefab is, no? The sprite in my assets appears when I drag it into the hierarchy.

Show more comments

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

126 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 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 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Instantiating an object randomly withing the boundaries of an object. 0 Answers

How to make transparent shader (2D)? 1 Answer

A Sprite move like a racecar, can it be done? 1 Answer

Having trouble with LoadAll 1 Answer

Unity - Cut a sprite in 2 halfs using the form of another sprite 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