0
\$\begingroup\$

I have two scripts. I create a reference to my instance of SoulManagement from Soul, but I can not access it's method, and I can not figure out why. Why can't I access my method?


SoulManagement

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SoulManagement : MonoBehaviour 
{
    int Souls;

    void SoulPlusMinus(int SoulValue)
    {
        Souls += SoulValue;
    }
}

Soul

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Soul : MonoBehaviour 
{
    public SoulManagement soulmanager;
    public int SoulValue;

    void Start()
    {
        soulmanager = FindObjectOfType<SoulManagement> ();
    }
    void OnTriggerStay2D()
    {
        soulmanager.SoulPlusMinus (SoulValue);
    }
}
\$\endgroup\$
0

1 Answer 1

1
\$\begingroup\$

Making Methods Accessible

When we create methods, we often must explicitly state the accessibility. A private method can only be accessed from within the class1, while a public method can be accessed from outside of the class. If you do not specify the accessibility, this will default to private. If you later deal with inheritancy, it is worth noting that a protected method will remain private, while still allowing access to classes that derive from that class.

In this case, we have a method signature of void SoulPlusMinus(int SoulValue). From the provided example, you want to use publicvoid SoulPlusMinus(int SoulValue).


Easy Access for Management Classes

With a name like SoulManagement, we can infer that this class manages your Soul classes. Classes like this are often designed in a way that only one instance should be running. In other words, you would not have many soul managers; you would only have one.

If this description fits your intended functionality, creating a singleton pattern may provide additional benefit, and extend your access to the class methods. A singleton pattern helps assure that we retain the one instance of the class, and by creating a static self-reference, we can then make that class accessible from anywhere. You can read more on singleton patterns over at Stack Overflow Documentation.


This is your SoulManagement class as a singleton variation:

public class SoulManagement : MonoBehaviour 
{
    ///<summary>Self-reference should always point to our active instance.</summary>
    public static SoulManagement instance;

    void Awake()
    {
        // If we do not currently have a SoulManagement instance, 
        if(instance == null)
        {
            // This is our SoulManagement instance; link the instance reference.
            instance = this;
        } 
        else if(instance != this)
        {
            // If we DO have an instance reference, and it is NOT this instance,
            // We already have a SoulManagement class; destroy this instance.
            Destroy(gameObject);
        }
    }

    int Souls;

    public void SoulPlusMinus(int SoulValue)
    {
        Souls += SoulValue;
    }
}

Using this approach, you can simply call SoulManagement.instance.SoulPlusMinus() from anywhere, without having to set up a direct reference to the instance.

\$\endgroup\$
2
  • \$\begingroup\$ I just found this out myself. I feel so stupid. \$\endgroup\$
    – Insurikai
    Commented Jan 22, 2017 at 10:46
  • \$\begingroup\$ @Insurikai, dont worry about it, have to learn somehow. This is not, by far, comparable to stupid mistakes I have made in the past. \$\endgroup\$
    – Gnemlock
    Commented Jan 22, 2017 at 10:51

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .