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 W1k3 · Mar 25, 2014 at 11:28 PM · c#errorarray

Object reference not set to an instance of an object error C#

Just an error that I don't posses the skill to fix. The error is on line 63.

 using UnityEngine;
 using System.Collections;
 
 public class floorbuilder : MonoBehaviour {
 
     //floors
     public GameObject[] fourrooms;
     public GameObject[] threerooms;
     public GameObject[] tworooms;
     public GameObject[] onerooms;
     public GameObject[] startrooms;
     GameObject[] currentfloor4;
     GameObject[] currentfloor3;
     GameObject[] currentfloor2;
     GameObject[] currentfloor1;
     //data containment
     public static int floornumb = 1;
     float doornumb;
     float roomnumb = 4; 
     float fourdoornumb;
     float threedoornumb;
     float twodoornumb;
     float onedoornumb;
     public int Size_Increase_Factor = 3;
     //positional variables
     int xnumb = 0;
     int znumb = 0;
     float snumb = 0;
     float rnumb = 0;
 
     void Start () {
         transform.position = new Vector3 (0, 0, 0);
         buildnextfloor ();
     }
 
     void buildnextfloor(){
 
         //Math determining the number of rooms and doors relitive to the floor level and shape of room positioning
         roomnumb = /*(insert mathy stuff stuff here)*/ (roomnumb + roomnumb) * Size_Increase_Factor;
         fourdoornumb = (Mathf.Round(roomnumb * (Random.Range(0.2f, 0.5f))));
         threedoornumb = (Mathf.Round(roomnumb * (Random.Range(0.1f, 0.4f))));
         twodoornumb = (Mathf.Round(roomnumb * (Random.Range(0.1f, 0.3f))));
         onedoornumb = (Mathf.Round(roomnumb * (Random.Range(0.1f, 0.3f))));
         doornumb = (fourdoornumb * 4) + (threedoornumb * 3) + (twodoornumb * 2) + (onedoornumb);
         roomnumb = (fourdoornumb) + (threedoornumb) + (twodoornumb) + (onedoornumb);
 
         if (doornumb % 2 == 1) {
             onedoornumb ++;
 
             doornumb = (fourdoornumb * 4) + (threedoornumb * 3) + (twodoornumb * 2) + (onedoornumb);
             roomnumb = (fourdoornumb) + (threedoornumb) + (twodoornumb) + (onedoornumb);
         }
 
         Debug.Log ("Number of rooms: " + roomnumb);
         Debug.Log ("Number of doors: " + doornumb);
         Debug.Log ("4doors: " + fourdoornumb); 
         Debug.Log ("3doors: " + threedoornumb);
         Debug.Log ("2doors: " + twodoornumb);
         Debug.Log ("1door: " + onedoornumb);
 
         //Selection of rooms
         for (int i = 0; i < fourdoornumb; i++) {
             currentfloor4[i] = fourrooms[(Random.Range(0, fourrooms.Length))]; 
         }
         for (int i = 0; i < threedoornumb; i++) {
             currentfloor3[i] = threerooms[(Random.Range(0, threerooms.Length))]; 
         }
         for (int i = 0; i < twodoornumb; i++) {
             currentfloor2[i] = tworooms[(Random.Range(0, tworooms.Length))]; 
         }
         for (int i = 0; i < onedoornumb; i++) {
             currentfloor1[i] = onerooms[(Random.Range(0, onerooms.Length))]; 
         }
 
         snumb = (Mathf.Round((Mathf.Sqrt (roomnumb))));
         rnumb = roomnumb - (snumb * snumb);
         snumb = snumb * 100;
         rnumb = rnumb * 100;
 
         //placing of rooms
 
         Instantiate (startrooms[Random.Range(0, startrooms.Length)], transform.position, transform.rotation);
         updatelocation ();
 
         for(int i = 0; i < fourdoornumb; i ++){
             Instantiate(currentfloor4[i], transform.position, transform.rotation);
             updatelocation();
         }
         for(int i = 0; i < threedoornumb; i ++){
             Instantiate(currentfloor3[i], transform.position, transform.rotation);
             updatelocation();
         }
         for(int i = 0; i < twodoornumb; i ++){
             Instantiate(currentfloor2[i], transform.position, transform.rotation);
             updatelocation();
         }
         for(int i = 0; i < onedoornumb; i ++){
             Instantiate(currentfloor1[i], transform.position, transform.rotation);
             updatelocation();
         }
     }
 
     void updatelocation(){
         if(xnumb < snumb){
             xnumb += 100;
         }
         else if (znumb < snumb){
             znumb += 100;
             xnumb = 0;
         }
         else{
             znumb += 100;
         }
 
         transform.position = new Vector3 (xnumb, 0f, znumb);
     }
 }
 

Any help would be awesome.

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 Simon-Larsen · Mar 25, 2014 at 11:42 PM 0
Share

Did you add any GameObjects to "fourrooms"?

avatar image W1k3 · Mar 25, 2014 at 11:45 PM 0
Share

alt text

There are three blender models in each public array.

capture.png (4.4 kB)

1 Reply

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

Answer by Simon-Larsen · Mar 26, 2014 at 12:44 AM

After looking at your code again I can see the error lies in your fields. You have not yet initialized your GameObject arrays

  GameObject[] currentfloor4;

When must initiatlize your array, you must know how many elements you're gonna use, example:

 GameObject[] gameObjects = new GameObject[10] // array lenght of 10

You can also initialize the arrays in the Start() function and in that function determine the length you're going to need on that array.

Now, while I have not thoroughly read through your code, my suggestion is that what you're looking for is actually a dictionary. I've found this page to hold some nice information about just that http://robotduck.wordpress.com/2009/11/04/88/

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 W1k3 · Mar 26, 2014 at 02:47 AM 0
Share

I initialized my room arrays, and for the arrays that need to vary in size, I used array lists. $$anonymous$$y scripts kinda sorta works now! thanks!

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

Random NullReferenceException 0 Answers

Distribute terrain in zones 3 Answers

NullReferenceException error in an array of objects 0 Answers

Error CS0029 Help? (Screenshot of Exact Error) 1 Answer

Object reference not set to an instance of an object 1 Answer


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