using UnityEngine;
namespace Crash{
public class HealthManager : Monobehaviour{
void Start(){}
void Update(){}
}
}
gameObject.GetComponent<DestructibleObject>()?.DestructObject();
Singleton
when you know there will be only 1 of something.namespace Crash{
public class LoggingManager : Singleton<LoggingManager> {
}
}
Singleton
should be named with XManager
e.g. LoggingManager
Singleton
can then be accessed quite easily/quickly from any scriptvoid Update(){
if(Input.GetKeyDown(KeyCode.Alpha0)){
LoggingManager.instance.UpdateColumn(keyCodeColString, "0 Pressed");
}
}
camera.main.transfrom
void OnCollisionEnter(Collision collision){
if(collision.gameObject.GetComponent<BossManager>() != null){
Debug.Log("Collided with boss");
}
}
namespace Crash {
public class ResourcePathConstants : MonoBehaviour {
// Materials
public static string MaterialFolder = "Materials/";
public static string OutlineSnapColliderMaterial = MaterialFolder + "OutlineSnapCollider";
}
}
// Example line of how it would be used in another file
Material outlineMaterial = Resources.Load<Material>(ResourcePathConstants.OutlineSnapColliderMaterial) as Material;
Feel like you are having race conditions (https://searchstorage.techtarget.com/definition/race-condition)? These are usually initialization order issues and result in Null Reference Exceptions
. Most likely this can be solved by one of the following steps
Awake()
instead of Start()
, reason found here: https://stackoverflow.com/questions/34652036/awake-and-start
Awake() occurs before Start() in Unity and should not do anything except getting variables. Note that all components that you get using GetComponent() should already exist before the Awake function is callednamespace Crash{
public class Ammo{
List<Bullet> bullets;
public List<Bullet> GetBulletList(){
if(bullets == null){
bullets = new List<Bullet>();
// add other initialization steps here
}
return bullets;
}
}
}