Thanks for the help folks! Yet another problem solved!
Can't figure this out! C# access another class
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);
}
}
Answer by Fluffy_Kaeloky · Dec 16, 2016 at 08:49 PM
PlayerEvents.AddToNuts();
PlayerEvents is your object's type, either make your AddtoNuts function and your int GoldNuts static or get an PlayerEvents reference.
Answer by SoraMahiro · Dec 16, 2016 at 08:51 PM
put this at the top of GoldenNutCollect: PlayerEvents playerEvents
then in OnTriggerEnter: playerEvents = FindObjectOfType<PlayerEvents>().AddToNuts;
by top I mean in the constants area directly below the class. Just clarifying
You should avoid invoking costly function like FindObjectOfType in a function that might be often called, better to get a reference in the start. Unless he want to get the playerEvents from the CanCollect Collider, then he'll need to use CanCollect.GetComponent(); Also, make guards, check if it did found a PlayerEvents before dereferencing it. If the class is a singleton, better to use the singleton template with an Instance property, that way you avoid the FindObjectOfType.
Thanks for the information, I have managed to solve the problem!
Follow this Question
Related Questions
Calling Function from another script not working 1 Answer
Disable a group of scripts based on base class. 0 Answers
Multiple PowerUps (One ItemBox) 0 Answers
How would I be able to store a value that can be used later with a function that returns void? 1 Answer
(SOLVED) Accessing (string) arrays by enumerations from another class 0 Answers