Search Unity

Creating my first name space

Discussion in 'Scripting' started by Shadowing, Sep 12, 2020.

  1. Shadowing

    Shadowing

    Joined:
    Jan 29, 2015
    Posts:
    1,648
    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):
    1.  
    2. using UnityEngine;
    3.  
    4. namespace WebRequest{
    5.  
    6.    public class Web : MonoBehaviour  {
    7.  
    8.       public void ServerRequest(){
    9.          // My method
    10.       }
    11.  
    12.    }
    13. }
    14.  



    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using WebRequest;
    4.  
    5.  
    6. public class Options : MonoBehaviour {
    7.  
    8.      void Awake(){
    9.          
    10.            Web.ServerRequest(); // An object reference is required for the non-static field
    11.        
    12.      }
    13. }
    14.  
     
  2. MaskedMouse

    MaskedMouse

    Joined:
    Jul 8, 2014
    Posts:
    1,092
    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
     
  3. astracat111

    astracat111

    Joined:
    Sep 21, 2016
    Posts:
    725
    Usually when you get that problem it means you need things to be static.
     
  4. Shadowing

    Shadowing

    Joined:
    Jan 29, 2015
    Posts:
    1,648
    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.
     
    Last edited: Sep 12, 2020
  5. adamgolden

    adamgolden

    Joined:
    Jun 17, 2019
    Posts:
    1,555
    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):
    1. public class Web : MonoBehaviour  {
    2.   public virtual void ServerRequest(){
    3.     // My method
    4.   }
    5. }
    6. // lets you override in another class..
    7. public class CustomWeb : Web {
    8.   public override void ServerRequest(){
    9.     // do whatever before..
    10.     base.ServerRequest();
    11.     // do whatever after..
    12.   }
    13. }
     
  6. Shadowing

    Shadowing

    Joined:
    Jan 29, 2015
    Posts:
    1,648
    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):
    1.  
    2.  
    3. public void ServerRequest(string Page, GameObject Window = null) {
    4.         ServerRequest(Page,Window);
    5. }
    6.  
    7.  
    8. public void ServerRequest(string Page, Button Button = null) {
    9.  
    10.         ServerRequest(Page,null,Button);
    11. }
    12.  
    13.  
    14. public void ServerRequest(string Page, GameObject Window = null, Button Button = null) {
    15.  
    16. //My main method
    17.  
    18.  
    19. }
    20.  
    21.  
    22.  
    23.  
     
  7. Shadowing

    Shadowing

    Joined:
    Jan 29, 2015
    Posts:
    1,648
    Oh i guess this example does work with static. I just don't use virtual. Didn't realize i can just do public only
     
  8. Vryken

    Vryken

    Joined:
    Jan 23, 2018
    Posts:
    2,106
    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.
     
    adamgolden likes this.