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 /
avatar image
0
Question by Rimonof · Aug 17, 2019 at 11:34 AM · classmethodcall

Call method from attached Gameobject

Hey, I just started learning Unity and C#.

In example below a Gameobject named HealthPotion with a script named HealthPotion is attached to the "public Gameobject itemGameobject".
On a button pressed, Use() in itemConsumable is called. In example below I wanna call Use() on HealthPotion via Use() on ItemConsumable without having to instantiate itemGameobject.(Use() is static)
When i type "HealhPotion.Use();" in Use() from ItemConsumable it works obviously, but i want this Dependent from attached Gameobject. So it should be somethink like "itemGameobject.Use()".
Hope my question is clear

 public class ItemConsumable : Item
 {
     public GameObject itemGameobject;
 
     public override void Use()
     {
 
     }

 public class HealthPotion : MonoBehaviour
 {
     
     public static void Use()
     {
         GameObject.FindGameObjectWithTag("Player").SendMessage("Heal", 3);
         Debug.Log("healed!");
     }
 
 }
 

Comment
Add comment · Show 1
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 sacredgeometry · Aug 17, 2019 at 12:00 PM 0
Share

There is a lot that you are doing slightly confused here. Let me write up an example with comments. One sec.

1 Reply

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

Answer by sacredgeometry · Aug 17, 2019 at 12:12 PM

Your Player Type

 public class MyPlayer : MonoBehaviour
 {
     public IItem CurrentItem;
     public int Health = 100;

     void Update()
     {
         if(CurrentItem != null && Input.GetKeyDown(KeyCode.Space))
         {
             CurrentItem.Use(this);
         }
     }
 }

Your item Interface

 public interface IItem
 {
     void Use(MyPlayer player);
 }

Your Item that implements the IItem interface

 public class HealthPotion : MonoBehaviour, IItem
 {
     public int Health;
 
     public void Use(MyPlayer player)
     {
         // Do item code and any destruction to item needed here
         player.Health += Health;
     }
 }

Comment
Add comment · Show 4 · 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 Rimonof · Aug 17, 2019 at 01:26 PM 1
Share

I did it a bit different but it works with Interface. Thanks!

avatar image sacredgeometry Rimonof · Aug 17, 2019 at 01:31 PM 0
Share

:) There are a million and one ways to do this. Implementing to interfaces is generally a good idea because it keeps things nice and loose/ decoupled.

avatar image sacredgeometry Rimonof · Aug 17, 2019 at 01:34 PM 0
Share

@Rimonof Also I was making you a video explanation haha I guess you dont need it anymore.

avatar image Rimonof sacredgeometry · Aug 17, 2019 at 01:59 PM 0
Share

Haha no. But thank you very much:)

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

110 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

Related Questions

Custom class functions - accessing one instance changes them all? 1 Answer

Weird error while trying to call a void from another class 1 Answer

Simple abstractions. Reusing code outside of class 1 Answer

Using method from main class in custom class 2 Answers

Can't use method from custom class during foreach 2 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