Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by ThisIsAJoke · Dec 26, 2012 at 02:31 AM · errorobject reference

Error: An object reference is required to access non-static member (Enumerations)

 using UnityEngine;
 using System.Collections;
 
 public class Click : MonoBehaviour {
 
     bool wasClicked = false;
     
     void OnMouseDown() {
           wasClicked = true;
           Activate();
     }
     
     public void Activate() {
            renderer.material.color = Color.red;
         
         for(int i=0; i < Rohstoffe.Anderes.Length; i++)
         {
             Debug.Log (Rohstoffe.Anderes[i]);
         }
         
         for(int i=0; i < Rohstoffe.Edelsteine.Length; i++)
         {
             Debug.Log (Rohstoffe.Anderes[i]);
         }
         
         for(int i=0; i < Rohstoffe.KraeuterPilze.Length; i++)
         {
             Debug.Log (Rohstoffe.Anderes[i]);
         }
     }
 }

Rohstoffe.cs and Click.cs are attached to my sample gameobject (a simple cube) and I get those errors which I can't solve myself: Assets/Scripts/Click.cs(35,46): error CS0120: An object reference is required to access non-static member `Rohstoffe.Anderes' (about six times)...

 using UnityEngine;
 using System;
 
 public class Rohstoffe : MonoBehaviour
 {
     public Enumeration.AnderesEnum[] Anderes;
     public Enumeration.EdelsteineEnum[] Edelsteine;
     public Enumeration.KraeuterPilzeEnum[] KraeuterPilze;
 }

 using UnityEngine;
 using System;
 
 public class Enumeration : MonoBehaviour
 {
     public enum AnderesEnum
     {
         Kupfer = 0,
         Eisen = 1,
         Silber = 2,
         Gold = 3,
         Mithral = 4,
         Arundur = 5,
         
         Baumwolle = 6,
         Haselnuss = 7,
         Walnuss = 8,
         Distel = 9,
         Oliven = 10,
         Mohn = 11,
         
         Basalt = 12,
         Granit = 13,
         Lehm = 14,
         Marmor = 15,
         Sand = 16,
         Ton = 17,
         
         Fichte = 18,
         Zalantharbaum = 19,
         Wehrbaum = 20,
         Suthbaum = 21,
         Sumpfzypresse = 22,
         Drachenbaum = 23,
         Umbubaum = 24,
         
         Flachs = 25,
         Hanf = 26,
         Bambus = 27,
         
         Hirschfell = 28,
         Wolfsfell = 29,
         Pantherfell = 30,
         Worgfell = 31,
         Baerenfell = 32,
         Tigerfell = 33,
         
         Dachsblut = 34,
         Wolfsblut = 35,
         Pantherblut = 35,
         Worgblut = 36,
         Baerenblut = 37,
         Adlerblut = 38,
         Eulenblut = 39,
 
         Adlerfedern = 40,
         Eulenfeder = 41,
         Seidenraupe = 42,
         Skelettknoechel = 43,
         Spinnendruese = 45,
     };
     
     public enum EdelsteineEnum
     {
         Amaratha = 50,
         Amethyst = 51,
         Aquamarin = 52,
         Aventurin = 53,
         Beljuril = 54,
         Bergkristall = 55,
         Bernstein = 56,
         Chrysokoll = 57,
         Citrin = 58,
         Diamant = 59,
         Falkenaugen = 60,
         Feueropal = 61,
         Gruftjade = 62,
         Iol = 63,
         Jacinth = 64,
         Jade = 65,
         Kanariendiamant = 66,
         Katzenauge = 67,
         Koenigstraene = 68,
         Koralle = 69,
         Kroetenstein = 70,
         Kunzit = 71,
         Lapislazuli = 72,
         Malachit = 73,
         Mandarin = 74,
         Obsidian = 75,
         Onyx = 76,
         Phenalope = 77,
         Rubin = 78,
         Saphir = 79,
         Sardonyx = 80,
         Schurkenstein = 81,
         Schwarzopal = 82,
         Smaragd = 83, 
         Sternensaphir = 84,
         Topas = 85,
     };
     
     public enum KraeuterPilzeEnum
     {
         Aloe = 100,
         Arkasu = 101,
         Arlan = 102,
         Arunya = 103,
         Baerentatze = 104,
         Brokwilb = 105,
         Brunnenkresse = 106,
         Chronchinis = 107,
         Eichhase = 108,
         Eisenkraut = 109,
         Goldrute = 110,
         Hexenroehring = 111,
         Hiradwurz = 112,
         Johanniskraut = 113,
         Katzenpfoetchen = 114,
         Koenigskerze = 115,
         Lathan = 116,
         Maultierpollen = 117,
         Merrig = 118,
         Nachtschatten = 119,
         Nebelkappe = 120,
         Pantherpilz = 121,
         Riesenbovist = 122,
         Teufelskraut = 123,
         Teufelszunge = 124,
         Tigerritterling = 125,
         Tintenfischpilz = 126,
         Trudustbluete = 127,
         Weidenroeschen = 128,
         WeisserAndorn = 129,
         Wolfskraut = 130
     };
 }
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

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by MarkFinn · Dec 26, 2012 at 05:17 AM

There are two separate problems in your code, the first of which is easily resolved, but the second will require some understanding on your part.

First, enums do not need to be inside a class.

Destroy the class Enumeration completely and just leave the enums by themselves.

 using UnityEngine;
 using System;
 
 public class Rohstoffe : MonoBehaviour
 {
     public AnderesEnum[] Anderes;
     public EdelsteineEnum[] Edelsteine;
     public KraeuterPilzeEnum[] KraeuterPilze;
 }
 
     public enum AnderesEnum
     {
        Kupfer = 0,
        Eisen = 1,
        Silber = 2,
        Gold = 3,
        Mithral = 4,
        Arundur = 5,
 
        Baumwolle = 6,
        Haselnuss = 7,
        Walnuss = 8,
        Distel = 9,
        Oliven = 10,
        Mohn = 11,
 
        Basalt = 12,
        Granit = 13,
        Lehm = 14,
        Marmor = 15,
        Sand = 16,
        Ton = 17,
 
        Fichte = 18,
        Zalantharbaum = 19,
        Wehrbaum = 20,
        Suthbaum = 21,
        Sumpfzypresse = 22,
        Drachenbaum = 23,
        Umbubaum = 24,
 
        Flachs = 25,
        Hanf = 26,
        Bambus = 27,
 
        Hirschfell = 28,
        Wolfsfell = 29,
        Pantherfell = 30,
        Worgfell = 31,
        Baerenfell = 32,
        Tigerfell = 33,
 
        Dachsblut = 34,
        Wolfsblut = 35,
        Pantherblut = 35,
        Worgblut = 36,
        Baerenblut = 37,
        Adlerblut = 38,
        Eulenblut = 39,
 
        Adlerfedern = 40,
        Eulenfeder = 41,
        Seidenraupe = 42,
        Skelettknoechel = 43,
        Spinnendruese = 45,
     };
 
     public enum EdelsteineEnum
     {
        Amaratha = 50,
        Amethyst = 51,
        Aquamarin = 52,
        Aventurin = 53,
        Beljuril = 54,
        Bergkristall = 55,
        Bernstein = 56,
        Chrysokoll = 57,
        Citrin = 58,
        Diamant = 59,
        Falkenaugen = 60,
        Feueropal = 61,
        Gruftjade = 62,
        Iol = 63,
        Jacinth = 64,
        Jade = 65,
        Kanariendiamant = 66,
        Katzenauge = 67,
        Koenigstraene = 68,
        Koralle = 69,
        Kroetenstein = 70,
        Kunzit = 71,
        Lapislazuli = 72,
        Malachit = 73,
        Mandarin = 74,
        Obsidian = 75,
        Onyx = 76,
        Phenalope = 77,
        Rubin = 78,
        Saphir = 79,
        Sardonyx = 80,
        Schurkenstein = 81,
        Schwarzopal = 82,
        Smaragd = 83, 
        Sternensaphir = 84,
        Topas = 85,
     };
 
     public enum KraeuterPilzeEnum
     {
        Aloe = 100,
        Arkasu = 101,
        Arlan = 102,
        Arunya = 103,
        Baerentatze = 104,
        Brokwilb = 105,
        Brunnenkresse = 106,
        Chronchinis = 107,
        Eichhase = 108,
        Eisenkraut = 109,
        Goldrute = 110,
        Hexenroehring = 111,
        Hiradwurz = 112,
        Johanniskraut = 113,
        Katzenpfoetchen = 114,
        Koenigskerze = 115,
        Lathan = 116,
        Maultierpollen = 117,
        Merrig = 118,
        Nachtschatten = 119,
        Nebelkappe = 120,
        Pantherpilz = 121,
        Riesenbovist = 122,
        Teufelskraut = 123,
        Teufelszunge = 124,
        Tigerritterling = 125,
        Tintenfischpilz = 126,
        Trudustbluete = 127,
        Weidenroeschen = 128,
        WeisserAndorn = 129,
        Wolfskraut = 130
     };
 

This will fix the error you post, but this uncovers another error, which is that you are trying to use an uninstantiated class as an object.

If you understand the above sentence, then well and good. You just need to use GetComponent(typeof(Rohstoffe)) on your gameobject to get the Rohstoffe object you work on.

If you don't understand the sentence about "uninstantiated class" and "object" then you need to do some reading on the basic rules and techniques of Object Orientated Programming. Far too much to fit in an answer here.

Comment
Add comment · Show 1 · 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 MarkFinn · Dec 26, 2012 at 05:54 AM 0
Share

public void Activate() { renderer.material.color = Color.red; Rohstoffe r = (gameObject.GetComponent(typeof(Rohstoffe))as Rohstoffe);

    for(int i=0; i < r.Anderes.Length; i++)
    {
      Debug.Log (r.Anderes[i]);
    }

    for(int i=0; i < r.Edelsteine.Length; i++)
    {
      Debug.Log (r.Edelsteine[i]);
    }

    for(int i=0; i < r.$$anonymous$$raeuterPilze.Length; i++)
    {
      Debug.Log (r.$$anonymous$$raeuterPilze[i]);
    }
 }</pre>

avatar image
0

Answer by ThisIsAJoke · Dec 26, 2012 at 12:15 PM

Thank you a lot! I do understand what you want to explain, it has been a long time since I coded the last time, but now I remember :) Merry Christmas!

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

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

10 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

Related Questions

Trying to get javascript to chnge UI.text but it wont 2 Answers

NullReferenceException... 1 Answer

An object reference is required when checking for active object 1 Answer

Object Reference Error On Play Audio 1 Answer

Enumerations in custom inspector C# 2 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