Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
This question was closed Dec 17, 2016 at 01:44 AM by Footknight for the following reason:

Thanks for the help folks! Yet another problem solved!

avatar image
0
Question by Footknight · Dec 16, 2016 at 08:46 PM · c#classes

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);
     }
 }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

  • Sort: 
avatar image
0
Best Answer

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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;

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image SoraMahiro · Dec 16, 2016 at 08:52 PM 0
Share

by top I mean in the constants area directly below the class. Just clarifying

avatar image Fluffy_Kaeloky · Dec 16, 2016 at 08:56 PM 1
Share

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.

avatar image Footknight · Dec 17, 2016 at 01:43 AM 0
Share

Thanks for the information, I have managed to solve the problem!

Follow this Question

Answers Answers and Comments

280 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges