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 Fignewticus · Apr 14, 2014 at 06:50 PM · c#spriteprefabs

How to Instantiate Sprite Prefab C#? - [SOLVED]

Hello all, I have been struggling with what I thought would be a very trivial concept, and it has proven to be quite frustrating.

So, I am trying to create a simple 2D game right now in which you control a character that can shoot spells of some sort. Currently, I am having trouble instantiating prefabs of a fireball spritesheet.

I have used the sprite importer / editor to slice up my sprite sheet into appropriately-sized sprites.

I then dragged all of the sprites I planned on using onto to the Game screen, then dragged them all into the Prefabs folder so that their names turned blue and became prefabs.

I then have a script on an empty game object called SpellSpawner that has a public array of GameObjects called spells[] which I have set to 3. I dragged 3 of the prefabs I wanted to use into each individual game object spot in the list. However, whenever I try to call or instantiate an object from the list (e.g. spells[0]) it gives me a null reference error.

Could anyone help me understand what I am doing wrong? Here is my code: using UnityEngine; using System.Collections;

 public class SpellCast : MonoBehaviour {
     
 
     //reference to hero
     private GameObject hero;
     private HeroControl control;
 
     //array to hold particles for different spells
     public GameObject[] spells;
 
     void Start () {
         hero = GameObject.Find ("hero");
         control = (HeroControl) hero.GetComponent (typeof(HeroControl));
 
     }
 
     // Update is called once per frame
     void Update () {
         if (Input.GetButtonDown ("Fire1") && control.facing == "right") {
             GameObject spellBolt;
             spellBolt = Instantiate(spells[0], transform.position, transform.rotation) as GameObject;
             spellBolt.transform.position = transform.position;
 
         }
     }
 }

And here is a picture of the whole situation in Unity: alt text

unityprefabbug.png (106.2 kB)
Comment
Add comment · Show 3
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 Fignewticus · Apr 14, 2014 at 08:02 PM 0
Share

Aaaaand now I feel like an idiot haha. Thank you so much. I swear these little syntax errors get me 90% of the time.

avatar image Malfegor · Apr 15, 2014 at 06:17 AM 0
Share

I know the feeling. Happy it helped!

avatar image Benproductions1 · Apr 15, 2014 at 06:31 AM 0
Share

Ins$$anonymous$$d of rena$$anonymous$$g the question and just adding "[solved]" at the end, you should close it.

1 Reply

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

Answer by Malfegor · Apr 14, 2014 at 06:58 PM

Try capitalizing the 'h' in your hero = GameObject.Find("hero"). So try making it 'hero = GameObject.Find("Hero")'. Maybe your hero is just null?

Comment
Add comment · 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

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

22 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Is there a way to change the sprite for all instances of the same object simultaneously? 1 Answer

Instantiated Sprite is not rendering. 1 Answer

Instantiated prefab doesn't collide with player 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