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 Laischen · Jun 30, 2014 at 09:55 PM · errorrigidbodyarrays

2D Array Rigidbody details storing error - C#

I am building this project, scattering planets across space randomly and I want Gravitation to be implied accurately. What I am doing is this: 1 Instantiating a random number of planets and scattering them randomly. 2 The actual planets in game should be stored in the first column of my Rigidbody Array (Dw code coming soon) 3 The rest of the columns should just be virtual references to these planets, think of it like having the exact same thing, but just not the planet in game. Example : someArray[somenumber,somenumberaswell] In the first column, that is somenumber=0, the actually visible Rigidbodies in game are stored. In the rest of the columns, their properties are stored. Like the second, third fourth and so on...: someArray[1,somenumberaswell]= some rigidbody's mass; Now here is the code, it is rather long so I put in some helpful comments:

  using UnityEngine;
         using System.Collections;
         public class Gravity : MonoBehaviour {
             public GameObject PlanetCore; //yes it HAS a rigidbody :D
             public static int pnum;
             public static Rigidbody[,] myPlanets;
                 public GameObject[,] obj;
             public Vector3[,] centerofmass;
             public int referencer,looper;
             public Transform[,] objT;
             public float[,] distanceBetweenPlanets;
             void Start () 
             {    
                 pnum = Random.Range(10,15); //a random number of planets
                 myPlanets = new Rigidbody[pnum,pnum] ; //they are both set pnum for the reason I proposed
                 obj = new GameObject[pnum,pnum];
                 objT = new Transform[pnum,pnum];
                 centerofmass=new Vector3[pnum,pnum];
                 radtodeg=new float[pnum];
                 Vector3 planetPosition;
                 //Instantiates planets and scatters them randomly :
                 for(looper=0;looper<myPlanets.GetLength(0);looper++){
                     if(1+looper==1){//so this only runs ONCE
                     for(int looper2=0;looper2<myPlanets.GetLength(0);looper2++){
                         planetPosition = new Vector3(Random.Range(-30f,30f),Random.Range(-30f,-30f),Random.Range(-30f,30f));
                         obj[0,looper2] = (GameObject)Instantiate(PlanetCore,planetPosition,Quaternion.Euler((Random.value+1)*360f,(Random.value+1)*360f,(Random.value+1)*360f));
                         }
                     }
                 //Destroys the planet which was being used to duplicate:
                 Destroy(PlanetCore);
                 looper=1;//looper set to 1 because the 0 column is already set
                 //This is where the problems start: Specifically in the first line under the loop header. so probably in every other line in the loop as well.
                 for(referencer=0;referencer<myPlanets.GetLength(0);referencer++){
                     myPlanets[0,referencer]=obj[looper,referencer].rigidbody;
                     myPlanets[0,referencer].mass=(Random.value+1)*4000f;
                     objT[0,referencer]=obj[looper,referencer].transform;
                     centerofmass[0,referencer]=obj[looper,referencer].rigidbody.worldCenterOfMass;
                     if(referencer+1==myPlanets.GetLength(0)){
                         looper++;
                         referencer=0;
                     }
                 }
                 //Just names the planets:     Planet0, Planet1, etc etc...
                     for(int i=0;i<myPlanets.GetLength(0);i++){
                         obj[0,i].name = "Planet"+i;
                     }
             }
         }
 
 

Now the error I keep getting right under that for loop header is this: Object reference not set to an instance of an object. And I keep staring at it and it stares at me and I am like WHY?!

Thanks a lot in advance, I know the code is rather long and might not get an answer, but thanks ! :D

Comment
Add comment · Show 1
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 Laischen · Jul 01, 2014 at 10:00 AM 0
Share

Not a bump

1 Reply

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

Answer by RakshithAnand · Jul 01, 2014 at 09:59 AM

Please use Classes to store property values and also generic Dictionaries are very useful if you know the type before hand Just put this code in a script called "Planets".

It should do the same job you are trying to do but with a very much better way. Refer to comments for what i am doing. If any help needed ask me

Hope this helps.

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class Planets : MonoBehaviour {
     // Use a prefab
     public GameObject planetPrefab;
 
     int numberOfPlanets;
     Vector3 randomPosition;
     Vector3 randomRotation;
     Dictionary<GameObject, PlanetProperties> myPlanets;
 
     // Use this for initialization
     void Start ()
     {
         numberOfPlanets = Random.Range (10, 15);
         myPlanets = new Dictionary<GameObject, PlanetProperties> ();
 
         // Keep your start method readable
         createRandomPlanets ();
 
         // Now you have a dictionary of planets with their properties.
         // You can loop through the dictionary and set different properties if you want
         int i = 0;
         foreach(KeyValuePair<GameObject, PlanetProperties> myPlanet in myPlanets)
         {
             i++;
             myPlanet.Key.name = "SomeRandomPlanet"+i;
             myPlanet.Value.mass = 500000f; 
             //etc ..
         }
 
     }
 
     void createRandomPlanets()
     {
         for(int i =0; i<numberOfPlanets; i++)
         {
             randomPosition = new Vector3(Random.Range(-30f,30f),Random.Range(-30f,-30f),Random.Range(-30f,30f));
             randomRotation = new Vector3((Random.value+1)*360f,(Random.value+1)*360f,(Random.value+1)*360f);
             GameObject newPlanet = Instantiate(planetPrefab, randomPosition, Quaternion.Euler(randomRotation) ) as GameObject;
             PlanetProperties newPlanetPorperties = new PlanetProperties();
             newPlanetPorperties.mass = (Random.value+1)*4000f;
             newPlanetPorperties.transform = newPlanet.transform;
             newPlanetPorperties.centerOfMass = newPlanet.rigidbody.worldCenterOfMass;
             newPlanetPorperties.position = newPlanet.transform.position;
             newPlanetPorperties.planetRef = newPlanet;
             // Here i kept a reference to the newly created planet since the dictionaries key is used to just acces the value which is planet properties.
             // so keeeping the reference to planet you can modify its name etc..
             
             myPlanets.Add(newPlanet, newPlanetPorperties);// add this new planet with detils to the dictionary
         }
     }
 }
 
 public class PlanetProperties
 {
     public float mass;
     public Transform transform;
     public Vector3 centerOfMass;
     public Vector3 position;
     public GameObject planetRef;
     // You can add more properties here like name, color, temp etc
 }
Comment
Add comment · Show 6 · 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 Laischen · Jul 01, 2014 at 10:04 AM 0
Share

I will most definitely give it a try! Thanks I know it was a rather boring question, and I really appreciate your help!

avatar image Laischen · Jul 01, 2014 at 12:00 PM 0
Share

Ok I tried it and it works PERFECT! Thanks man if I could I'd rate you up but I can't yet. Thanks A LOT! :D

avatar image RakshithAnand · Jul 01, 2014 at 12:19 PM 0
Share

You are welcome :) .. try to rate it up once you can ;)

avatar image Laischen · Jul 01, 2014 at 12:25 PM 0
Share

I just noticed that the mass of each planet stays at the default of 1 when instantiated, but it should be different for each planet... - Oh I found the problem, see when you called PlanetProperties you were setting the mass value for only that variable and not for the instantiated object... I wrote it like this ins$$anonymous$$d: GameObject newPlanet = Instantiate(planetPrefab, randomPosition, Quaternion.Euler(randomRotation) ) as GameObject; newPlanet.rigidbody.mass = (Random.value+1)*4000f; PlanetProperties newPlanetPorperties = new PlanetProperties(); newPlanetPorperties.mass = newPlanet.rigidbody.mass;

avatar image RakshithAnand · Jul 01, 2014 at 12:31 PM 0
Share

I did that because i just showed you a way of storing the properties using classes. Yes if you want to set the mass there itself you can do it like that or with the same code you can add a line before adding the planet to dictionary:

newPlanet.mass = newPlanetProperties.mass; // Will give you a proper idea from where the mass value is co$$anonymous$$g

But anyhow both ways are fine!

Show more comments

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Using arrays and for loop with GetComponent error 1 Answer

the object of type rigidbody has been destroyed 3 Answers

How to make colliders without using any of the inbuilt physics 1 Answer

Check for rigidbody presence when in collision with a trigger 2 Answers

Scoring System ArgumentOutOfRangeException Error 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