I am having some issues with Classes; still getting my head around OOP (pointers to good tutorials welcome - the one’s I’ve read weren’t so helpful).
Anyway, I’ve been trying to get this working for about an hour now - simply put - I want to access the gameobject in my script/class SnapToGrid
.
This object is called from deploy_machine class which inherets from MonoBehaviour (if I understand that bit right)
public class deploy_machine : MonoBehaviour {
private SnapToGrid snapper;
void Awake () {
snapper = new SnapToGrid(GameObject.Find(this.name));
}
}
With my SnapToGrid looking like:
public class SnapToGrid {
public GameObject game_object;
public SnapToGrid(GameObject game_object){
//Transform cached_transform
SetupRigidbody();
ConstructTrigger();
game_object.renderer.material = green_material;
}
private void SetupRigidbody(){
Rigidbody rb = game_object.rigidbody; //the rigidbody component of this object
}
}
The problem is it fails inside SetupRigidbody (line 11 above).
NullReferenceException: Object reference not set to an instance of an object
So how do I structure/declare my new class to get access to the gameobject?
Thanks.