how do i create a global array of game objects from a class? (python sample code)

(sorry, not good at explaining)

basically im trying to create a physics simulation of a solar system but in order to do so i need to create an array that both contains all objects in the system and can be accessed by them so i can calculate forces. I would know how to do this in python :

Class Body:
    def __init__(self, size, mass, position):
        self.size=size
        self.mass=mass
        self.position=position

Class World:
    def __init__(self, Count):
        self.bodies=[]
        for i in range(Count):
            size=input("enter size")
            mass=input("enter mass")
            position=input("initial position")
            newBody=Body.__init__(size, mass, position)
            self.bodies.append(newBody)
        update()
            

    def update(self):
        for n in self.bodies:
            for x in self.bodies:
                """calculate force of x on n""" 
            """apply forces to n"""

but i have no idea how to do this in unity.

1 Answer

1

ALSO take into account that global variable doesnt exist, the most “similar” it is the keyword static. What static means is that rather than each object has his own variable, statics variables will be shared across all instance

public class World : MonoBehaviour
{
   Body[] bodies;
   public InputField inputField;//THIS IS JUST AN EXAMPLE YOU NEED TO GET THE DATA

   public Awake()
   {
       int count = 3;
       bodies = new Body[count];

       //YOU WILL NEED TO GET THE SIZE MASS AND POS IN A DIFFERENT WAY LIKE AN INPUTFIELD
       var size = inputField.value;
       var pos= inputField.value;
       var mass= inputField.value;          

       for(int i = 0; i < count; i++)
       {
           bodies *= new Body(size,pos,mass);//ALL OBJECTS WILL HAVE SAME POS SIZE AND MASS FIND A WAY OF GETTING THEIR VALUE*

}
}
public void Update()
{
foreach(Body body in bodies)
{
Debug.Log(“YOUR STAFF IN THE UPDATE”);
}
}
}

public class Body
{
float size;
float mass;
float position;

Body(float size, float mass, float position)
{
this.size = size;
this.mass = mass;
this.position = position;
}
}