I have a Gameobject that is duplicated multiple times. It has a script called playerscript attached to it.
As my code has grown quite a bit now I thought it might be a idea to make another class within that script to try & keep things a bit better organised.
Now when I try & call getsplineinfo from playerscript I get a null reference exception due to the debug.log & I daresay a lot of other functions will not work either.
I thought my 2nd class would have all the functionality of the orignal class, actually as Im typing this out I’ve realized that my object only has the playerscript attached to it. So I probably need to reference the gameobject somehow then the script so hanldesplines gets access to everything that playscript has access to.
What would be a good way to achieve that.
using UnityEngine;
using System.Collections;
using System.Linq;
using System.Collections.Generic;
public class playerscript : MonoBehaviour {
// my original class
// approx 120 methods in here
//
}
public class handle_splines : MonoBehaviour {
// my 2nd class
public void getspline_info()
{
Debug.Log ("NAME = " + gameObject.name);
}
}
ok thanks, so I could either just attach another script to the gameobject & access that from the original script OR have even more methods / variables in my original script.
Just to point out, having 2 classes in the same file is fine. Having 2 monobehaviours is not. If these were pure c# classes it would be no issue.
The issue is the engine uses the file to work out what component it is.
So in the future if you want something like this, first have a think if it requires being a monobehaviour (awake, on enable, start, update etc) or if it can be just a c# class, which has much less overhead.
Also you can run an update from a pure class by giving it a tick or update method and passing in delta time from another monobehaviours update. We have an update manager for this so not everything needs to be a component, only things that make sense for it.
e.g.
void UpdateTick(float deltaTime)
{
do some delta time stuff
}
and then just call that from any monobehaviour with update and pass in Time.deltaTime (you can actually use Time.deltaTime directly but I prefer to pass the single context to everything for keeping track purposes)