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 .Create · Apr 29, 2014 at 02:11 AM · nullreferenceexceptionarrays

NullReferenceException - Array of Transforms?

hello to all, sorry for my english, it is terrible, i know. I'm a very newbie with unity, and i'm making an android game. My problem is on my shooting script, i've isn't even finished him yet and i'm already getting errors D=

Here is my code:

 using UnityEngine;
 using System.Collections;
 
 public class ShooterBehaviour : MonoBehaviour {
 
 
     public Transform Bullet;
     public int bulletNumber;
     public float fireRate;
     public float currentRate;
     Transform[] bulletList;
 
     void Start () {
         Transform[] bulletList = new Transform [bulletNumber];
         for (int i = 0; i < bulletNumber; i++){
             bulletList[i] = Instantiate (Bullet, transform.position, transform.rotation) as Transform;
         }
     }
 
     void Update () {
         if(Input.GetKeyDown("space")){
             Debug.Log("was pressed!");
             Debug.Log("its time to fire!");
             bulletList[1].gameObject.SetActive(true);
         }
     }
 }

When i press the space bar, i was supposed to fire the first bullet from my array, but instead, i get one nullReferenceException. Does anyone can help me?

Comment
Add comment · Show 4
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 robertbu · Apr 29, 2014 at 02:17 AM 0
Share

Please paste the error message from the console and/or show us what line is generating the error. You did initialize 'Bullet' by drag and drop in the Inspector?

avatar image getyour411 · Apr 29, 2014 at 02:18 AM 0
Share

Does bulletNumber have a value in the Inspector?

avatar image .Create · Apr 29, 2014 at 02:36 AM 0
Share

Thanks for helping guys. Yes, bulletNumber is 20 in my inspector, and when i press play, i can see the 20 bullets instantiating. The error i get is:

NullReferenceException: Object reference not set to an instance of an object ShooterBehaviour.Update () (at Assets/Scripts/ShooterBehaviour.cs:25)

avatar image Jeff-Kesselman · Apr 29, 2014 at 02:37 AM 0
Share

In general, when you get an exception or other error yo unwed to show people the whole error exactly as printed by the system so they can help you. When its an exception you need to give the entire stack trace.

1 Reply

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

Answer by robertbu · Apr 29, 2014 at 03:19 AM

Your bug is on this line 14:

    Transform[] bulletList = new Transform [bulletNumber];

You declare 'bulletList' on line 8. Because you use 'Transform[]' at the start of line 14, you declare a local variable of the same name. This local variable hides the variable at the top of the file, so you never initialize the 'bulletList' that is used on line 24. The fix is to remove 'Transform[]' from the start of Line 14:

    bulletList = new Transform [bulletNumber];



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 .Create · Apr 29, 2014 at 03:26 AM 0
Share

Dude, what a newbie mistake :T Btw, That is correct, thank you!

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

21 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

Related Questions

Attempt at creating an array of text files results in different errors 1 Answer

Object is null when it shouldn't be. 1 Answer

Why Again Arrays In ExecuteInEditMode() Gives NullReferenceException Errors?? 1 Answer

NullReferenceException happens only sometimes without cause? 2 Answers

Null Ref Exception on a script that should have a reference in it? 2 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