Wondering if someone could help me out with this Getting the message. Not sure what im doing wrong here An object reference is required for the non-static field Code (csharp): using UnityEngine; namespace WebRequest{ public class Web : MonoBehaviour { public void ServerRequest(){ // My method } } } Code (csharp): using UnityEngine; using WebRequest; public class Options : MonoBehaviour { void Awake(){ Web.ServerRequest(); // An object reference is required for the non-static field } }
Your class Web is a regular monobehaviour component. So you need a reference to an instance like the error tells you. But if you want it to be accessible from anywhere, you have to remove MonoBehaviour from your web class and make it static as well as your method. If you're new to programming, I'd suggest doing a few tutorials
Thanks for the replies guys. Nah not new to programming just never made a name space before. Whats the point of doing using WebRequest; if i have to make it static. The reason I don't make it static is im trying to use virtual and it said I can't use virtual as a static method.
If you want to call Web.ServerRequest(); then you need to change public void ServerRequest(){ to public static void ServerRequest(){. Otherwise, you need to call ServerRequest through a reference to an instance of Web. Also, "trying to use virtual" sounds to me like you'd like to override ServerRequest in another class. If that's want you want to do.. // using virtual in your base class.. Code (CSharp): public class Web : MonoBehaviour { public virtual void ServerRequest(){ // My method } } // lets you override in another class.. public class CustomWeb : Web { public override void ServerRequest(){ // do whatever before.. base.ServerRequest(); // do whatever after.. } }
Thanks for the reply @polemical I see people doing this all the time with out using static. All i have to do is just use using NameSpace at the top of my script and i have access to all the public functions in that name space. Let me start over to show what I'm originally trying to do here. Trying to make it where the last argument can either be a GameObject or a Button Code (csharp): public void ServerRequest(string Page, GameObject Window = null) { ServerRequest(Page,Window); } public void ServerRequest(string Page, Button Button = null) { ServerRequest(Page,null,Button); } public void ServerRequest(string Page, GameObject Window = null, Button Button = null) { //My main method }
Oh i guess this example does work with static. I just don't use virtual. Didn't realize i can just do public only
Namespaces are simply just a way to logically group named types together; they have absolutely no influence on code functionality. When you include a namespace in a script, it just means you're using a type in that namespace. This is why, for instance, every time you declare a List, HashSet, Dictionary, Queue, or some other type of collection, you have to import the System.Collections.Generic namespace. All of those objects are collections that iterate over a generic type, so someone at Microsoft decided it made sense to group them together under this namespace. Namespaces also allow you define types that already exist elsewhere. There is a type named Debug in both the UnityEngine and System.Diagnostics namespaces, for instance. If both types were in the same namespace, or not in any namespace, that's when a compiler error would be thrown. This is also why if you were to import both the UnityEngine and System.Diagnostics namespaces in a script, you'd get an ambiguous-type error if you were to type Debug somewhere, because it isn't known if you meant to use UnityEngine.Debug, or System.Diagnostics.Debug, and you'd have to explicitly describe which one you meant by writing them just like that.