- Home /
An Inheritance question. ( accessing non-static members from a script component)
Hi i'm struggling with an aspect of unity involving inheritance.
I initially assumed the best way to work would be to derive new classes from GameObjects but as they are a sealed class it has throw a little confusion into the way I would work. I then understood that prefabs were essentially the way around this, you could bundle up your own bespoke GameObjects in the form of prefabs (is this correct) now the problem comes when accessing members of instances of those prefabs. I have set up a bit of code which essentially looks like this.
I have tried it with getComponent on (Cube) and ("Cube") on the instance and it doesn't show that the isActive bool variable is available for setting.
Thank you in advanced
//----------------------------------------------------------------------------------------
// First Script
//----------------------------------------------------------------------------------------
public class GameItem : MonoBehavior
{
public bool is Active;
}
//----------------------------------------------------------------------------------------
// Second Script ~ Is on a Cube Prefab
//----------------------------------------------------------------------------------------
public class Cube : GameItem
{
isActive = true;
}
//----------------------------------------------------------------------------------------
// Third Script
//----------------------------------------------------------------------------------------
public class CubeEffector : MonoBehavior
{
public GameObject cubePrefab; // Holds a reference to the Cube Prefab
GameObject cubeInstance = Instantiate(cubePrefab, transform.position, transform.localRotation);
if(conditions are met){
cubeInstance.isActive = false;
}
}
Answer by DaveA · Aug 14, 2011 at 05:08 PM
try (although there are other syntaxes):
Cube c = (Cube)cubeInstance.GetComponent(typeof(Cube));
c.isActive = false;
wouldnt that just instantiate the cube class without the prefab? i need the prefab because the geometry is part of the prefab ideally cube and the prefab would be the same thing, is there a way to do this? I assumed the best way was to attack a script to the prefab and instantiate the prefab as if you would the class.
thank you for your reply btw
Answer by AaronG · Aug 14, 2011 at 05:53 PM
You might have to place the 'isActive == true' declaration inside the Awake() function for that class.
isActive isnt being shown as being available in code completion and throws up an error if i try, its completely out of scope.