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 CharlieLama · Oct 15, 2018 at 11:28 AM · raycastraycasthitinheritanceinheritinherited-members

Inheritence problems with raycast

I am making an inventory system. So i made an inheritence script calles Items, and i have object that inherit from that. I made different scripts; cubes, spheres and capsules. I have a raycast on click, and then i tell my raycast to look for the item hit. I made a function in the Item base script and then override it in the child scripts. But it always sees it as a cube no matter what i do. I have a cube with the script cubes on it and a capsule with Capsules.

This is my item script;

     public virtual GameObject IHaveItem(GameObject currentItem)
 {
     print("base class enter");
     return currentItem;
 }

and this is my child inheritence:

 public GameObject cubeTex;

 public override GameObject IHaveItem(GameObject currentItem)
 {
     print("cube override");
     currentItem = cubeTex;
     return currentItem;
 }

i do the same for the other two (spheres and capsules)

This is where i look which item i am clicking on:

 public void WhichItem()
 {
     if (Physics.Raycast(transform.position, transform.forward, out hit, 50.0f))
     {
         Debug.DrawRay(transform.position, transform.forward, Color.red, 50.0f);
         if (hit.collider.GetComponent<Items>())
         {
             print("hit an object");
             items.IHaveItem(currentItem);
             print("current item 1 =" + currentItem);
             currentItem = items.IHaveItem(currentItem);
             print("current item 2 =" + currentItem);
             LookForSpace();
             hitItemSC = true;
         }
     }
 }


Can someone please help me? i tried a lot of stuff and i really cant figure this one out. If you dont know but have an idea thats good to, im willing to try them. i just dont wanna lose my inheritence part

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 eses · Oct 15, 2018 at 02:01 PM 0
Share

@CharlieLama - IHaveItems are just your methods.... not full classes, which would have been helpful to see. Also, calling IHaveItem will return a GameObject.

avatar image eses · Oct 15, 2018 at 02:11 PM 0
Share

@CharlieLama

Your script logic feels somewhat unclear, why are you calling your single item "Items" ins$$anonymous$$d of "Item"?

Also, why each item has IHaveItem method that receives some GameObject? In your WhichItem method, it does not make any sense, at least to me, what you are doing. You first raycast, then if your hit has Items component, you pass some "currentItem" to your item? What is this current item and why are you passing it? You definitely don't need to pass your item anything to see what it is, you should be checking the type of hit object.

avatar image CharlieLama eses · Oct 15, 2018 at 02:44 PM 0
Share

Yes, iHaveItems is only a method but also my full class, there is nothing else in there

because Items is an inherit class and cubes inherit from Items, and capsules and Spheres too Yes it is because i am making an inventory system, i want to have the item i just hit, so i can spawn a texture linked to my hit object

1 Reply

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

Answer by CharlieLama · Oct 15, 2018 at 03:04 PM

Thanks to @eses i realised a silly issue

Instead of currentItem = items.IHaveItem(currentItem);

i used currentItem = hit.transform.gameObject;

seemed to fix everything for me

Comment
Add comment · 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

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

195 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 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 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 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

Issues with detecting collision with objects that have certain tags using Raycast 0 Answers

MoveToInteraction takes 1 argument 1 Answer

RayCast enemy hp 0 Answers

Raycast not detecting hit with collider 0 Answers

is there a way to set a objects direction to a normalized direction. 0 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