Hello everyone,
I have this game design / game object issue. I will sum up the logical structure of my game and next present the problem itself.
In the game there are several classes, two of which are Cube and CubeGenerator. CubeGenerator is attached to empty CubeGenerator gameobject and responsible for generating cubes on the playground given methods specified within the class. When the moment to generate a cube comes up, the following code is being run:
cube[cubeNo] = gameObject.AddComponent("Cube") as Cube;
cube[cubeNo].Init (new Vector3(checkX, checkY+0.5f, checkZ),new Vector3(checkX, checkY, checkZ), AssignTower(), AssignOwner ());
Which in essence creates multiple identical Cube components on CubeGenerator GO. Init() method is my personal override for inability to call constructors - it assigns default values and performs initial checks, AND calls another metod which is responsible for instantiating actual cubes. What I want to achieve is to have Cube class OVER a GameObject class - so that gameObject is like a “physical” being, inheriting what I want from the uber class.
The problem I have is once I instantiate cubes, I can’t (or don’t know how) to communicate from the level of GameObject to the Cube which created it (i.e. cube[CubeNo]).
I have tried rewriting the above code into this:
cubeGoArray[cubeNo] = new GameObject();
cubeGoArray[cubeNo].AddComponent("Cube");
cube[cubeNo] = cubeGoArray[cubeNo].GetComponent<Cube>() as Cube;
so that Cube class is added as a component to each cube GO, but still something is off. I can tell that by the fact that I have problem with events. Namely:
void OnCollisionEnter(Collision col)
{
if (col.gameObject.name.StartsWith ("projectile"))
{
Debug.Log ("uderzylo " + gameObject.name);
Debug.Log ("sila " + strength);
IncrementTimesHit();
renderer.material.color = Color.blue;
if (strength >0)
{
strength -= timesHit;
}
}
}
doesn’t work = i.e. fields like “strength” are not being inherited somehow (although they work - since they are embedded in the name of GO, and I can tell that they are passed to GO from Cube class), but instructions which affect GO and not Cube work (like “renderer.material.color = Color.blue;”).
I want to have cube objects as instants of Cube class, which then are instantiated as cube GameObjects AND remember their structure and properties from Cube class (as assigned to them by CubeGenerator during creation and updated later on).
So, my final question is, how to rewrite the code to amend this?