Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
1
Question by ebrown38 · Nov 28, 2016 at 06:52 PM · instantiatearrayfor-loop

Object reference not set to an instance of an object with object set,

I am having an issue with getting my code to run properly because of the error "Object reference not set to an instance of an object." From what I have seen, this is due to there not being an object set, as the error suggest. However, I do have an object set, it just doesn't seem to recognize it. Here is my code:

 public GameObject ChaserModel;
 public Transform[] OriginPoint;       
 private GameObject[] Chasers;
   
     void Start() {
         for (int i = 0; i < OriginPoint.Length; i++) {
            Chasers[i] = Instantiate(ChaserModel);
             Chasers[i].transform.position = OriginPoint[i].transform.position;
             
 
 
         }
         Destroy(ChaserModel);
 
     }

So, in this part of the code, I want to create an instance of ChaserModel and attach it to an array called Chasers, then, when created, I want it to be set to a position on the OriginPoint Array, then finally, it will destroy the original model. The problem is at the line Chasers[i] = Instantiate(ChaserModel); It will create one clone of the object, then throw the NullRefrenceException error. It also won't move the Chasers model to the point it is supposed to move it to.

,

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
1

Answer by wachief · Nov 28, 2016 at 08:02 PM

Hello,

You are missing a very small but crucial line.

You Instantiate an object, and that works fine, but then you try to put it in an array. That array is never constructed and is there for null. The line you are missing is

 Chasers= new GameObject[size];

Size is the size of the array you want to create. In your case it would be OriginPoint.Length so the line would look like this

 Chasers= new GameObject[OriginPoint.Length];

With your code it will look like this:

 public GameObject ChaserModel;
  public Transform[] OriginPoint;       
  private GameObject[] Chasers;
    
      void Start() {
         Chasers= new GameObject[OriginPoint.Length];
          for (int i = 0; i < OriginPoint.Length; i++) {
             Chasers[i] = Instantiate(ChaserModel);
              Chasers[i].transform.position = OriginPoint[i].transform.position;
          }
          Destroy(ChaserModel); 
      }

I hope this will help

Comment
Add comment · Show 2 · 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 BrunoBelmonte · Nov 28, 2016 at 08:08 PM 0
Share

You are right about setting a size for the array, but i still get the same error of @ebrown38 even with this code. It only works with me if i remove "private" from "GameObject[] Chasers;"

avatar image wachief BrunoBelmonte · Nov 29, 2016 at 01:25 PM 0
Share

This script throws no errors for me. $$anonymous$$aybe you are doing something wrong in the editor. do you have all the point in OriginPoint array set? ( I would rename it to OriginPoints has its a Collection)

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

6 People are following this question.

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

Related Questions

Instantiate object in for loop with array 2 Answers

Can't assign an instantiated class to an array? 1 Answer

adding "X" amount of objects to an array 2 Answers

Prefabs instantiated from an array are keeping their public int value 1 Answer

Material[] Object reference not set when instantiating 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