Accesing enum type from another script
Hello again :)
I am currently trying to get an enum type from another script. I am trying to get it via Raycast.
My problem at the moment is that i declared the enum in the script to acces it, but now it wants me to declare the variable in the inspector rather than getting it from the raycasthit. Which I understand, but I just don't know how to change that.
I'd be thankful for everyone able to tell me how I can acces the enum type before my switch statement :)
This is the script:
using UnityEngine;
using System.Collections;
public class Pickup : MonoBehaviour {
Camera mainCam;
Collider other;
public Inventory inventory;
public ItemType type;
void Start(){
mainCam = GetComponent<Camera>();
}
void Update (){
int x = Screen.width / 2;
int y = Screen.height / 2;
RaycastHit hit;
Ray ray = mainCam.ScreenPointToRay(new Vector3(x, y,0));
Debug.DrawRay (ray.origin, ray.direction * 10, Color.yellow);
if (Input.GetMouseButtonUp(0) && Physics.Raycast(ray, out hit, 50) && hit.collider.gameObject.tag == "item") {
hit.collider.gameObject.GetComponent<Item>(ItemType);
switch(type){
case ItemType.STUHL:
Destroy(hit.transform.GetComponent<Collider>());
hit.transform.gameObject.active = false;
inventory.AddItem(hit.collider.gameObject.GetComponent<Item>());
break;
case ItemType.PAPIER:
Debug.Log("Papier!");
break;
}
}
}
}
Answer by corn · Jan 11, 2016 at 09:33 PM
All right, so judging from line 25 ( hit.collider.gameObject.GetComponent<Item>(ItemType);
), it looks like your Item class has an ItemType field and you want to find its value, correct ?
In that case, the correct syntax would be :
type = hit.collider.gameObject.GetComponent<Item>().type;
With type
being whatever you named the ItemType field of your Item class.
However, it is good practice to check if GetComponent did find an Item, better safe than sorry.
Item hitItem = hit.collider.gameObject.GetComponent<Item>();
if (hitItem == null)
{
return;
}
type = hitItem.type;
Hope that's what you needed.
Yes that is exactly what I needed and it works perfectly! :D Thanks for always helping me out! You're awesome! :D
Your answer
Follow this Question
Related Questions
(Raycasts) C# Why am I shooting myself? 0 Answers
Is there any way how to do raycasts like this? 0 Answers
Strange Raycast problem 0 Answers
Scripting Errors 1 Answer