An object reference is required to accsess a non-static member 'Lives.loselife()'

code for loose collider

using UnityEngine;
using System.Collections;

public class LoseCollider : MonoBehaviour{

private Lives lives;

void Start(){

	lives = GameObject.FindObjectOfType<Lives>();
	
}

void OnTriggerEnter2D(Collider2D trigger){

	Lives.loseLife();
	
}

}

Code for Lives

using UnityEngine;
using System.Collections;

public class Lives : MonoBehaviour {

public Sprite[] liveSprites;

private int lives;
private int liveIndex;
private LevelManager LevelManager;

void start() {

	LevelManager = GameObject.FindObjectOfType<LevelManager>();
	lives = 3;

}

public void loseLife() {

	lives--;
	assignSprite();

}

void assignSprite() {

	liveIndex = lives-1;
	this.GetComponent<SpriteRenderer>().sprite=liveSprites[liveIndex];

}

}

Change Lives.loseLife(); to lives.loseLife();.


WARNING: Technical explanation below that you don’t really need to worry about if you’re just beginning.


Why? Lives (capitalized) refers to your Lives class. lives (lowercase) refers to an instance of your Lives class that’s named lives.

When you call Lives.loseLife();, you’re trying to call loseLife() from the class itself. Since it’s public void loseLife() and not public static void loseLife(), it doesn’t belong to the class itself. It belongs to instances of the class.

I hope that makes sense! Check out the Unity - Statics tutorial for more information.

Thanks so much thanks for the explanation too @MattG54321