Hey folks! I am trying to make a script so when I walk over an object it adds it to the score counter (provided it’s the correct object)

I have a script on the object and a script on the player. The player has the GUI stuff and the object simply has detection and then tries to tell the other script to add to the points. Ugh this is hard to explain! Just have a look please!

Script on the object:
using UnityEngine;
using System.Collections;

public class GoldNutCollect : MonoBehaviour {
	
	public Collider CanCollect;
	
	
	void StartHere () {

	}
	
	//What Happens when the player enters the collider
	void OnTriggerEnter(Collider CanCollect) {
			
			Debug.Log("The Player has collected a gold nut!");
			PlayerEvents.AddToNuts();
			Destroy(gameObject);
	}
}

Once the nut is collected, it should refer to the other class and do the increment. But its throwing the ol’
error CS0120: An object reference is required to access non-static member `PlayerEvents.AddToNuts()’
nonsense and I cannot for the life of me figure out why it will not work.

Here is the PlayerEvents script:

using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class PlayerEvents : MonoBehaviour {
	
	bool EscText = false;
	public int GoldNuts = 0;
	

	// Use this for initialization
	void Start () {
	
	}

	IEnumerator ShutDown(){
		EscText = !EscText;
		yield return new WaitForSeconds(3);
		EscText = !EscText;
		yield break;
	}
	
	void AddToNuts() {
		GoldNuts ++;
	}
	
	// Update is called once per frame
	void Update () {
		if (Input.GetButtonDown("Escape")){
			StartCoroutine(ShutDown());
			Application.Quit();
		}
	}
	
	void OnGUI () {
		if(EscText){
			GUILayout.Label("Shutting Down!");
		}
		GUILayout.Label("Nuts:" + GoldNuts);
	}
}

PlayerEvents.AddToNuts();

PlayerEvents is your object’s type, either make your AddtoNuts function and your int GoldNuts static or get an PlayerEvents reference.

put this at the top of GoldenNutCollect: PlayerEvents playerEvents
then in OnTriggerEnter: playerEvents = FindObjectOfType<PlayerEvents>().AddToNuts;