Hello. I’m currently working on an enemy kill box that causes a respawn to last checkpoint when hit. Prior to setting up the script for the kill box, my checkpoint code worked fine. Now, with the kill box script referencing my function that restarts the level and reverts to last checkpoint, it seems that the line of code that grabs my checkpoint position is giving me the following error message:
An object reference is required to access non-static member `UnityEngine.Component.transform’
Providing my scripts for reference.
My kill box script:
void OnTriggerEnter(){
StartCoroutine(ballHealth.RestartLevel ());
}
My character health script (Where the respawn code is located):
public float maxFallDistance = -10;
static AudioSource audio;
static bool isRestarting = false;
static public AudioClip GameOverSound;
void Start (){
audio = GetComponent<AudioSource> ();
}
void Update () {
if (transform.position.y <= maxFallDistance) {
//UnityEngine.SceneManagement.SceneManager.LoadScene("ballGame");
if (isRestarting == false) {
StartCoroutine(RestartLevel ());
}
}
}
public static IEnumerator RestartLevel () {
isRestarting = true;
audio.clip = GameOverSound;
audio.Play();
yield return new WaitForSeconds (audio.clip.length);
if (!checkpoints.isReached) {
UnityEngine.SceneManagement.SceneManager.LoadScene("ballGame");
}
transform.position = checkpoints.ReachedPoint; //This line of code is the one causing the error
isRestarting = false;
}
And the checkpoint script:
public static Vector3 ReachedPoint;
public static bool isReached;
void OnTriggerEnter (Collider col){
if (col.tag == "Player") {
if (transform.position.x > ReachedPoint.x) {
isReached = true;
ReachedPoint = col.transform.position;
}
}
}
Reading up on this error, I understand it has to do with static and non-static members. I’m new to the concept. I know variables and functions must be declared static in order to be accessed from other scripts (at least that’s my current understanding, of which I know there is more to it). But I’ve spent the better part of my evening trying to find a solution and am not quite there. If anyone has encountered this problem and knows what direction to point me in, I’d be mighty appreciative.