- Home /
Inheriting variables from common parent class on two different gameobjects
Here's the structure of my problem.
In my scene I have two snake gameobjects, each controlled by a player using a turn-based system. Each of these snake gameobjects contains a c# script with a class called "Snake_BodyCreator", which creates the snake's body. This class inherits from another class in another script, called "Snake", which contains all of the properties common to a snake. The "Snake" class is not attached to any gameobject in the scene.
I then have a third class called "Snake_Movement". This class is also attached to each of the gameobjects, and is enabled through my "TurnController" script, when it is a particular player's turn.
Here's how it looks in the inspector:
Snake1(Gameobject)
Snake_BodyCreator(Inherits from "Snake" class)
Snake_Movement(I want this to access the Snake_BodyCreator class' properties
Snake2(Gameobject)
Snake_BodyCreator(Inherits from "Snake" class)
Snake_Movement(I want this to access the Snake_BodyCreator class' properties
My problem is trying to access the "Snake_BodyCreator" script from the "Snake_Movement" script. Here's the relevant code from the "Snake_Movement" script that accesses the "Snake_Body" script:
public class Snake_Movement : MonoBehaviour {
Snake_BodyCreator snakeBody;
void OnEnable ()
{
snakeBody = gameObject.GetComponent<Snake_BodyCreator>();
if(snakeBody == null)
{
Debug.Log("Can't find Snake_BodyCreator script");
}
}
void MoveUp()
{
//If at the top of the grid
if(snakeBody.SnakeGridCoOrdXY[1] == gridPos.GetLength(1)-1)
{//Do something}
}
As you can see, I have a Debug.log check to make sure that "snakeBody" is actually accessing the "Snake_BodyCreator" script, and it is. But when I use it to try and access any public property from the "Snake_BodyCreator" script in the "MoveUp()" function (in this example, "SnakeGridCoOrdXY[1]"), I get a NullReferenceException error, stating that this reference is not set to an instance of an object. I don't understand how it isn't. I wondered if it might have something to do with the fact that I have two "Snake_BodyCreator" instances in the scene that inherit from the common "Snake" class.
Any answers would be much appreciated.
Matt
Answer by hypnoticmeteor · Jan 10, 2015 at 02:20 PM
I am not going to write the entire code. You need add all the programs that you use. So snake must be in the scene. Add an empty gameobject and assign Snake.cs to it. Make sure all the variables that you want to use in another script are public. The second script should now either find or reference this gameobject which has "Snake.cs" attached to it. This is done by
GameObject SnakeGO;//assign in editor
Snake snakePointer;//instance of the script
void Start()
{
//if assigned in editor use this
snakePointer = SnakeGO.gameObject.GetComponent<Snake>();
//if not assigned but is available in the editor
snakePointer = GameObject.Find("Snake").GetComponent<Snake>();
//if not in editor
//the error of null pops up
}
snakePointer.publicVariable;
Follow the same procedure for the others.
I thought that you could inherit from a class without having it present in the scene. $$anonymous$$y Snake_BodyCreator script inherits from it as below.
public class Snake_BodyCreator : Snake { //rest of the script }
Also, the "Snake_$$anonymous$$ovement" class isn't trying to inherit properties from the "Snake" class. It's trying to access things from the "Snake_BodyCreator" class.
Thanks, but you can. I solved the issue. Turns out that I hadn't declared my properties correctly in the "Snake_BodyCreator". It's perfectly possible to inherit from a class written in a script that isn't in the scene.
Thanks anyhow.
Your answer
Follow this Question
Related Questions
C# accessing an array from a script in a different gameobject outside of the awake function 0 Answers
Using GetComponent with a subclass 3 Answers
Difficulties accessing other components 4 Answers
Help with accessing scoring system variable C# plz. 3 Answers
NullReferenceException. GetComponent dose not work properly 2 Answers