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 maroltl · Mar 26, 2014 at 04:25 PM · getcomponentenumnullreference

access enum values

I have enum edecared in this script

  enum BlockType {Dirt=1, Copper=2, Iron=3, Gold=4}

and in this script I am trying to access enum value in materials script and compare it.

 function OnCollisionEnter(col : Collision)
     {
         if(col.gameObject.name == "block(Clone)")
         {   
                Materials = col.gameObject.GetComponent(materials);
                if(Materials.BlockType == Materials.BlockType.Dirt){
                    Debug.Log("You Hit Dirt");
           }
     }

it gives me this error

NullReferenceException: Object reference not set to an instance of an object

Thanks in advanced :)

Comment
Add comment · Show 3
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 Lo0NuhtiK · Mar 26, 2014 at 04:32 PM 0
Share

http://answers.unity3d.com/questions/15250/how-to-declare-and-use-a-enum-variable-in-javascri.html

avatar image sriram90 · Mar 26, 2014 at 05:40 PM 0
Share

How BlockType can be a member of $$anonymous$$aterial (unity material). I Guess you're trying to get material name and trying to compare it with enum BlockType and your file name is $$anonymous$$aterials.cs . can you post your entire cs file.

Need to know what you're trying with this line $$anonymous$$aterials = col.gameObject.GetComponent(materials);

avatar image maroltl · Mar 26, 2014 at 06:29 PM 0
Share

with this line I am assigning varible $$anonymous$$aterials to script materials, where enum is. here I give every object a random enum value and when I collide with objects, I want to get their given enum type. here is the main part of scipt "materials". All I want is to get object s enum type and compare it with other enum values.

     #pragma strict
     var block : GameObject;
     enum BlockType {Dirt=1, Copper=2, Iron=3, Gold=4}
     var curType = BlockType.Dirt;
     var curNumber = 0;
     var curPosition = 0; 
     
     var IronTxt : Texture2D;
     var GoldTxt : Texture2D;
     var CopperTxt : Texture2D;
     
     function Start () {
         curNumber = Random.Range(0,100);
         curPosition = block.transform.position.y * (-1);
         Random$$anonymous$$aterials();
     }
     
     function Random$$anonymous$$aterials(){
         if (curPosition <= 32){
             SelectTypeFirst();
         }else if(curPosition > 32 && curPosition <= 64){
             SelectTypeSecond();
         }else if (curPosition > 64 && curPosition <= 96){
             SelectTypeThird();
         }else if (curPosition > 96 && curPosition <=128){
             SelectTypeFourth();
         }
     }
 
 function SelectTypeFirst(){                                // med 0 in 32
     if(curNumber <= 70){
         curType = BlockType.Dirt;
     }
     if(curNumber > 70 && curNumber <= 90){
         curType = BlockType.Copper;
         renderer.material.mainTexture = CopperTxt;
     }
     if(curNumber <= 97 && curNumber > 90){
         curType = BlockType.Iron;
         renderer.material.mainTexture = IronTxt;
     }
     if(curNumber <= 100 && curNumber > 97){
         curType = BlockType.Gold;
         renderer.material.mainTexture = GoldTxt;
     }
 }
     

0 Replies

· Add your reply
  • Sort: 

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

22 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Need some help with a NullReference Error 3 Answers

error BCE0022: Cannot convert error, please help 3 Answers

Neater way to test an Enum in a different script? 1 Answer

Enumeration Equals Method Causing : NullReferenceException: Object reference not set to an instance of an object 0 Answers

(C#) How to access enum from a remote object? 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