Change the state of a GameObjects Child using an Abstract Class with an Interface
So I wanna make a farm style game which uses generic tiles that can be modified to being the different ground types like grass/hedges/soil/... In order to give them the ability to grow and later on other automated timer related stuff, I made an abstract class Tile which then is defined by several options using the IAutomatic interface. Now to make it grow my plan was to simply enable the current state of the plant in the game object. When I tried it directly in the Grass script everything worked fine but now after putting it inside the grow class, I get a "NullReferenceException" leading to "transform.Find(name+state).gameObject.SetActive(true);".
Do you have any idea/solution how to solve this?
And a big thank you in advance :)
Grass class:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Grass : Tile
{
private int state = 1;
public float minGrowTime = 5f;
public float maxGrowTime = 20f;
public string grassName = "Grass_State_";
private float timer;
Grass grass;
void Start()
{
timer = Mathf.Lerp(minGrowTime, maxGrowTime, Random.value);
grass = new Grass();
grass.setAutomation(new Grow());
timer = 1f;
}
void Update()
{
if (timer < 0)
{
grass.DoAutomation(state, grassName);
timer = Mathf.Lerp(minGrowTime, maxGrowTime, Random.value);
}
else
{
timer -= Time.deltaTime;
}
}
}
Abstract class:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public abstract class Tile : MonoBehaviour
{
IAutomatic automate;
public void setAutomation(IAutomatic automate)
{
this.automate = automate;
}
public void DoAutomation(int State, string Name)
{
automate.automatic(State, Name);
}
}
Interface:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public interface IAutomatic
{
void automatic(int state, string name);
}
public class Grow : MonoBehaviour, IAutomatic
{
public void automatic(int state, string name)
{
print(name+state);
transform.Find(name+state).gameObject.SetActive(true); //returns NullReferenceException
}
}
Screenshot of the Hierarchy:
Answer by Cornelis-de-Jager · Apr 10, 2018 at 04:09 PM
Short Answer:
// This Creates a grass object that is not attached to a Transform
Grass grass;
...
grass = new Grass ();
Meaning that any Unity/Mono related functions do not work. You cannot use transform, gameObject or any similar once used to interact with the game world.
Long Answer:
Your problem is you are misusing Unity classes and C# classes. They cannot be used interchangeably. I am talking about your Grow Class.
How do you know your class is a Unity class as appose to a C# class (Ok I know that one is derived from the other, this is just to enforce that there is a difference in usage), well that's easy:
/* Plain C# Class */
public class NormalClass {
// This is a normal class, it works how any class would in C#
// Doesn't interact with game wor;d
// Create the class with ' Class c = new Class () '
}
/* Unity Class */
public class NotNormalClass : MonoBehaviour { // <----- Mono is added
// Can Interact with game world, transform.move, GetComponent, tag etc..
// CANNOT BE CREATED WITH Class c = new Class !!!
// Created by .AddCompnent<Class> ()
// Must be attached to game gameobject
}
And your issue lies with your Grass class since 1. Its not a Class on its own so you cannot add it to your transform. So when you call transform.Find -- It can't find a transform since it is not attached to one.
// Here you only make the class, you never assigned it to a GameObject, you only reference it
Grass grass = new Grass();
You need to create a new Grass Class not part of the interface and attach it to the object. Then you can reference it with:
Grass grass = GetComponent<Grass> ();