i'am completely new to Unity and C#. I try to use my OOP knowledge to build something that holding the data and "business logic" of my game.
Game outline: Some cubes falling down, can be moved while falling and finally dropping on an pane.
The cubes are prefab objects and must be tracked to count the score, detect un/wanted behaviour, etc.
In the scene is:
- 1 empty GameObject = CubeSpanPoint + CubeSpawn Script
- 1 empty GameObject = GameMain + GameMain script (holding game settings and business logic)
- 1 prefab Cube + MoveScript + FallingScript
GameMain.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[System.Serializable]
public class GameMain : MonoBehaviour
{
private float CubeSpawnSpeed = 3.0f; //seconds
private int score = 0;
// construtor
public GameMain()
{
}
// getter: is falling allowed for the cube
public bool getFalling(){
return true;
}
// getter cube spawn speed
public float getCubeSpawnSpeed(){
return this.CubeSpawnSpeed;
}
}
The FallingScript must get the data from MainGame (getters), FallingSpeed and the falling permission.
CubeFalling.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BoxFalling : MonoBehaviour
{
public GameMain gameMain;
void Start(){
// an attempt to get the instance this way, but fail
// GameMain main = GameObject.Find("GameMain").GetComponent<GameMain>();
}
void Update()
{
// Is still falling allowed?
if (gameMain.getFalling()){
transform.Translate(Vector3.down);
}
}
}
But this produces the error NullReferenceException: Object reference not set to an instance of an object
BoxFalling.Update ()
It is also not possible to use the inspector by pulling the GameMain GameObject onto gameMain slot in the CubeFalling Script to the same named public variable.
What i've been expecting is that i've to pull the GameMain scipt to the GameMain GameObject, what is equal to an instantiation with new GameMain
. And to reuse this instance, that i've to pull the GameObject to the GameMain Variable in the falling script. But this don't work.