- Home /
Access objects enum type
Is there a way to detect enum type of object which my player collide with. I done lot of research, but I didnt find anything usefull.
An enum isn't something you collide with. Tell us more about what you are wanting to do.
$$anonymous$$aybe I didnt explained it good enough. I have lot of blocks and i assign random enum type to them. When my player collide with one of them i want to get the enum type of that specific block(cube) .
Answer by getyour411 · Mar 23, 2014 at 09:26 PM
From your collision you'll get the cube/gameObject you collided with. From that you can do something like
ScriptWithEnum scriptWithEnum = collision.gameObject.GetComponent<ScriptWithEnum>();
then you can do
if(scriptWithEnum.variableOfTypeEnum.Enum.Value == "....")
// do stuff
Obviously change those values to ones that fit your code.
Thanks for answer. I'll try it tomorrow since it is getting late here.
var $$anonymous$$aterials; function OnCollisionEnter(col : Collision) { if(col.gameObject.name == "block(Clone)") { $$anonymous$$aterials = col.gameObject.GetComponent(materials); if($$anonymous$$aterials.BlockType.Enum.Value == "Dirt"){ Debug.Log("HitDirt"); } } }
thats the code on which runs on collision. It gives $$anonymous$$e this error
NullReferenceException: Object reference not set to an instance of an object Boo.Lang.Runtime.RuntimeServices.GetDispatcher (System.Object target, System.String cache$$anonymous$$eyName, System.Type[] cache$$anonymous$$eyTypes, Boo.Lang.Runtime.DynamicDispatching.DispatcherFactory factory) Boo.Lang.Runtime.RuntimeServices.GetDispatcher (System.Object target, System.Object[] args, System.String cache$$anonymous$$eyName, Boo.Lang.Runtime.DynamicDispatching.DispatcherFactory factory) Boo.Lang.Runtime.RuntimeServices.GetProperty (System.Object target, System.String name) UnityScript.Lang.UnityRuntimeServices.GetProperty (System.Object target, System.String name) drilling+$OnCollisionEnter$3+$.$$anonymous$$oveNext () (at Assets/scripts/GameScripts/drilling.js:31)
materials is the script with enum in, and the Dirt is first type of enum.
this code logs objects enum type to console.so the problem is not in accessing the enum type, since its working perfectly. problem is in comparing objects enum type (curType) with other enum values.
function OnCollisionEnter(col : Collision)
{
if(col.gameObject.name == "block(Clone)")
{
$$anonymous$$aterials = col.gameObject.GetComponent(materials);
Debug.Log($$anonymous$$aterials.curType);
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613141007im_/https://answers.unity.com/themes/thub/images/avi.jpg)