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
0
Question by FuryFight3r · Mar 15, 2020 at 10:30 PM · instantiaterandomrandom.rangearray of gameobjects

Randomizing a selection from an array

I am trying to randomize the selection of 3 objects that get displayed on a computer, when running either code I am left with the dreaded "Object reference not set to an instance of an object." the objects are most certainly being found as without the array the items instantiate fine, but with an array I receive this error.

I have tried both this..

     void addSale()
     {
         count++;
         dummyTimer = addTimer;
         rand = Random.Range(0, adArray.Length);
         GameObject temp = Instantiate(adArray[rand], sales.transform);
     }

and this..

     void addSale()
     {
         count++;
         dummyTimer = addTimer;
         GameObject temp = Instantiate(adArray[Random.Range(0, adArray.Length)], sales.transform);
     }

and this...

     void addSale()
     {
         count++;
         dummyTimer = addTimer;
         int arrayLength = adArray.Length;
         rand = Random.Range(0, arrayLength);
         GameObject temp = Instantiate(adArray[rand], sales.transform);
     }

With exact results...

Again. these objects Instantiate fine without an array.... meaning that unity can find these objects without fail... untill I add the array, thats when unity can no longer reference the object that is trying to be instantiated, also the count is being added and the timer reset upon receiving the error.

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
Best Answer

Answer by Link17x · Mar 16, 2020 at 01:41 AM

So you've narrowed the problem down to the array, and it's producing a null reference exception. I can't tell the problem from the code, because I can't see the array.


Based on what you've said, the only suggestion I can really make is that you might not have initialized your array before using it. You must declare an array using the new keyword before you fill or access it. Have you done this?


If the answer is yes, then the array elements must be null. This means that you're probably not filling the array elements up properly, again, seeing the code for this would be useful to determine this. Since you haven't, I'd suggest making a foreach loop, go over your array and do a Debug.Log on each element to print the contents. Then you can confirm your array contains what you expect.

Comment
Add comment · Show 3 · 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 FuryFight3r · Mar 16, 2020 at 06:25 AM 0
Share

Hi Link17x, thank you for your answer, I dont understand what you mean by initializing the array before using, below I have put the entire script asnd a photo of the array.

EDIT: the problem was the Instantiate parent was not initialized.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class saleGenerator : $$anonymous$$onoBehaviour
 {
 
     [SerializeField]
     GameObject[] adArray;
     GameObject sales; ///////Error was here.. I had not initialized the transform that the instantiated prefabs spawn to..
 
     float addTimer = 20f;
     public float dummyTimer;
 
     public int count;
     public int rand;
 
     void Start()
     {
         dummyTimer = addTimer;
         sales = gameObject;
     }
 
     void Update()
     {
         if(dummyTimer > 0)
         {
             dummyTimer -= Time.deltaTime;
         }
         if(dummyTimer <= 0)
         {
             addSale();
         }
     }
 
     void addSale()
     {
         count++;
         dummyTimer = addTimer;
         int arrayLength = adArray.Length;
         rand = Random.Range(0, arrayLength);
         GameObject temp = Instantiate(adArray[rand], sales.transform);
     }
 
 }
 

alt text

avatar image valentingurkov FuryFight3r · Mar 16, 2020 at 06:31 AM 0
Share

What has happened to me is that sometimes such components are also attached to another object by accident and there your array has nothing. Can you double check and make sure no other object has this component on the scene

avatar image FuryFight3r valentingurkov · Mar 16, 2020 at 11:06 AM 0
Share

Hi thanks for the comment, however I have already fixed this, the prefabs and the instantiate side of things were fine, but the object i wished to parent the newly instantiated objects I had forgotten to initialize in the start funtion

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

154 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 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

I made this script to instantiate objects, but when the game starts I get this error:IndexOutOfRangeException: Array index is out of range. 1 Answer

Calling 2 specific random float numbers in random.range using array not working! 2 Answers

Generate random no and active that GO using array 1 Answer

Instantiate issues 1 Answer

How can I Instantiate and do it in a random spot in a random spot? 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