Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by AnniJ · Jan 11, 2016 at 06:36 PM · c#raycastenumtype

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;
             }
         }
     }
 }

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

1 Reply

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

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.

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 AnniJ · Jan 13, 2016 at 09:18 PM 0
Share

Yes that is exactly what I needed and it works perfectly! :D Thanks for always helping me out! You're awesome! :D

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

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

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

[SOLVED] get object which is nearby to mouse 1 Answer


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