-1

I keep getting this error, but I don't even know what is or isn't static in this context? I have tried solutions like setting instances and checking capitalization but I just get the same error. I want the shop script to change the monney value, which is written into debug until I set up the right U.I.

The Zoney script:

    using UnityEngine;
    using UnityEngine.UI;
    
    public class Zoney : MonoBehaviour
    {
        public Text Money;
        public int Monney;
        private string Mony;
        
        // Start is called before the first frame update
        void Start()
        {
            Money = GetComponent<Text>();
        }

        public void setMonney(int Change) 
        {
            Monney = Change;
        }   
        
        // Update is called once per frame
        void Update()
        {          
            Mony = Monney.ToString();
            Money.text = Mony;
        }
    }

The Shop script:

    using UnityEngine;
    
    public class Shop : MonoBehaviour
    {
        public int Change;
    
        // Start is called before the first frame update
        void Start()
        {
        }
    
        // Update is called once per frame
        void Update()
        {
          Change += 1;
          Zoney.setMonney(Change);
          Debug.Log(Change);
        }
    }
Milan Egon Votrubec
  • 3,696
  • 2
  • 10
  • 24
  • @hasi90 **NO!** It is "forbidden" to create instances of `MonoBehaviour` via the keyword `new`! See e.g. [I'm getting the warning: You are trying to create a 'MonoBehaviour' using the 'new' keyword](https://stackoverflow.com/a/62016464/7111561). Doing so will need to a lot of problems ;) – derHugo Feb 06 '21 at 13:38
  • 1
    The error is self-explanatory. If you want to call a non-static member, you need to use a reference to an instance of the class. See duplicate. Unity3d has some peculiarities regarding requirements for creating instances, so assuming you can't make the member static, you'll have to look at the Unity3d-specific advice to see how to acquire the reference you need. – Peter Duniho Feb 07 '21 at 05:22

3 Answers3

0

Because you're working with Unity, you need to follow the requirements of the engine.

In a case like this, where you need an instance of a component (MonoBehaviour), you really want to be referencing a Unity created instance, instead of creating a new one with the new keyword. Creating an instance of a component using the new keyword is going to leave you with an instance of the class that's not associated with any Unity GameObject.

The far more reliant way to get the component you want to reference, is to use the Inspector window, and drag into the object field, the right component on the desired object. In this case, I am going to assume you'll want to drag a Scene object (in your hierarchy) into the object field slot.

You would do that by first defining a variable. This can generally be done in one of two ways:

  1. public Zoney zoney;
  2. [SerializeField] private Zoney zoney;

In this example, once you've assigned your reference, use the variable zoney, instead of the class name Zoney. Note that your variable name could be anything else you feel is appropriate, e.g. _zoney, or myZoney.

Your new Shop script could then look like this:

    public class Shop : MonoBehaviour
    {
        public int Change;
        public Zoney zoney;
    
        void Update()
        {
          Change += 1;
          zoney.setMonney(Change);
          Debug.Log(Change);
        }
    }
Milan Egon Votrubec
  • 3,696
  • 2
  • 10
  • 24
-1

Zoney is a class, you need to create an instance of it first before using it.

Instantiate a class that derives from MonoBehaviour

Also very important, you need to update your shop object so it has the Zoney instance as a member object otherwise your updates to money wont be kept: i.e.

public class Shop : MonoBehaviour
{
    private Zoney;


    public int Change;

    // Start is called before the first frame update
    void Start()
    {
        _HiddenZoney = gameObject.Addcomponent<Zoney>();
    }

    // Update is called once per frame
    void Update()
    {
        Change += 1;
        _HiddenZoney.setMoney(Change);
        Debug.Log(Change);
    }
}

Thanks @derHugo for the alert!

Jon P
  • 117
  • 4
  • 1
    **NO!** It is "forbidden" to create instances of `MonoBehaviour` via the keyword `new`! See e.g. [I'm getting the warning: You are trying to create a 'MonoBehaviour' using the 'new' keyword](https://stackoverflow.com/a/62016464/7111561). Doing so will need to a lot of problems ;) – derHugo Feb 06 '21 at 13:37
  • Thanks! I must have been asleep at the wheel ;) – Jon P Feb 07 '21 at 20:56
-1

You need to create a object from Zoney class to access it's non static memebers. Try below:

public class Shop : MonoBehaviour
{

    public int Change;
    public Zoney myZoney; // Need to create the object
    // Start is called before the first frame update
    void Start()
    {
       
    }

    // Update is called once per frame
    void Update()
    {
        Change += 1;
      myZoney.setMonney(Change); // Access members using created object
      Debug.Log(Change);
    }
}
hasi90
  • 84
  • 8
  • **NO!** It is "forbidden" to create instances of `MonoBehaviour` via the keyword `new`! See e.g. [I'm getting the warning: You are trying to create a 'MonoBehaviour' using the 'new' keyword](https://stackoverflow.com/a/62016464/7111561). Doing so will need to a lot of problems ;) – derHugo Feb 06 '21 at 13:38