- Home /
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 :)
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);
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;
}
}