- Home /
instantiating unique classes of a type
I am trying to make a grand strategy game on the scale of planets. For this I want to be able to generate a semi randomised "planet" with a set of properties attached via a script.
atm my planet class looks like this:
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Planet : MonoBehaviour {
public int population;
public int Population {
get {
return population;
}
set {
value = population;
}
}
public int space;
public int Space
{
get
{
return space;
}
set
{
value = space;
}
}
public double survivability;
public double Survivability
{
get
{
return survivability;
}
set
{
value = survivability;
}
}
public string type;
public string Type
{
get
{
return type;
}
set
{
value = type;
}
}
I run in to problems when i am trying to attach a new planet script to the prefab when it is instantiated ive tried doing it like this:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlanetGen : MonoBehaviour {
int population;
int space;
string type;
double survivability;
public static void CreatePlanet(GameObject go, int population, int space, string type, double survivability)
{
Planet newPlanet = go.AddComponent<Planet>();
newPlanet.Population = population;
newPlanet.Space = space;
newPlanet.Type = type;
newPlanet.Survivability = survivability;
newPlanet.name = go.name;
}
void Start () {
System.Random r;
r = new System.Random();
space = r.Next(1, 100);
int di_Type = r.Next(0, 3);
switch (di_Type)
{
case 0:
type = "Terrestrial";
survivability = 1;
break;
case 1:
type = "Lava";
survivability = 0.4;
break;
case 2:
type = "Gaia";
survivability = 1.1;
break;
}
population = (int)System.Math.Round(0.25 * space * survivability);
CreatePlanet(gameObject, population, space, type, survivability);
}
}
But from this I am stuggling to find a way to make it so a script attached to the same object as this new instance can access and edit the variables. I would really like to be able to do it this way so that large random maps can be generated easily but if this method isn't gonna work id love to know. Ive looked around for hours but none of the refrences ive found really address what i want to do.
here's an example of what i would like to be able to do with my planet scripts once they're attached bare in mind I'm trying to get these scripts to be generalised in a sense so they will work on any instance of the prefab:
public class PlanetGrowth : MonoBehaviour {
float population;
void Start () {
InvokeRepeating("GrowPop", 2.0f, 10.0f);
}
void GrowPop() {
population = gameObject.GetComponent<Planet>().population;
if( population < gameObject.GetComponent<Planet>().Space ){
population = population + 1 + (1*(float)gameObject.GetComponent<Planet>().Survivability);
gameObject.GetComponent<Planet>().Population = (int) Mathf.Round(population);
print(population);
}
}
edit: atm an empty copy of the planet class is attached at least that is how it appears on the inspector at runtime
It seems to be a ti$$anonymous$$g problem :
If Unity doesn't call your PlanetGen.Start()
method before the PlanetGrowth.Start()
one you'll try to access a Planet
which is not already created.
Do you have any exception ?
For the design you want I would suggest your PlanetGen script to create a new GameObject per new Planet, so basically a Planet IS a GameObject
Your answer
Follow this Question
Related Questions
How to move the object to where the object is already pointing to? 1 Answer
How do I make a crouch script unity 3d 1 Answer
Enemy Health Script Working On One Enemy Not Multiples 2 Answers
Help with ontrigger enter and exit with UI Display appear and disappear 1 Answer
I'm trying to allow my character to only jump when grounded 1 Answer