Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 thornekey · Mar 12, 2014 at 10:45 PM · c#

Access script (not derived from mono) functions in another script

I have a script called playerActionsDesc, and im trying to access it from another script. The thing is, this script isn't on any object, and isn't deriving from mono.

heres what i have:

 using UnityEngine;
 using System.Collections;
 
 public class playerActionsDesc : InfoBox {
 
     public void eatFood() {
         addGameMessage("You eat the " + 
         "Bread" //to be changed to item names..
         + " and feel great.");
     }
 }

InfoBox is a script added to the player to tell them what theyre doing and the function addGameMessage works all good. The problem is trying to access it from another script called doPlayerActions..

     void eatingFood () {
         if (Input.GetKeyDown(KeyCode.Q)) { //test input
             this.GetComponent<playerActionsDesc>().eatFood();
         }
     }

this is the part that wont work..

"this.GetComponent().eatFood();" how else to call the function?

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 nesis · Mar 13, 2014 at 12:01 AM 0
Share

I'm not sure if this is the variable you want. Try gameObject.GetComponent < $$anonymous$$yClass > ().$$anonymous$$yFunction().

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by spraycanmansam · Mar 13, 2014 at 01:15 AM

If the script is not a MonoBehaviour attached to a scene game object, you need a way of creating and referencing an instance of it. Depending on the structure of your program, you could create an instance of PlayerActionsDesc in your DoPlayerActions script.

 public class DoPlayerActions() : MonoBehaviour
 {
     private PlayerActionsDesc _playerActionsDesc;
 
     private void Start() {
         _playerActionsDesc = new PlayerActionsDesc();
     }
 
     private void EatingFood() 
     {
         if(Input.GetKeyDown(KeyCode.Q)) {
             _playerActionsDesc.EatFood();
         }
     }
 }

That will work and is perfectly fine, but like I mentioned above, depending on the structure of your game you may want to look into singletons if you need to reference the PlayerActionsDesc class from other scripts but only want one instance of it.

Comment
Add comment · Show 8 · 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 thornekey · Mar 13, 2014 at 03:37 AM 0
Share

hi, thanks for your input, but it didnt work.. :S am i doing it wrong?

 private playerActionsDesc _playerActionsDesc;
 
     private void Start () {
         _playerActionsDesc = new playerActionsDesc();
     }
     
     private void eatingFood () {
         if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Q)) {
             _playerActionsDesc.eatFood();
             
         }
     }
avatar image thornekey · Mar 13, 2014 at 06:56 AM 0
Share

anyone? im not sure what the problem is now

avatar image spraycanmansam · Mar 13, 2014 at 10:07 AM 0
Share

In the EatFood() method of your PlayerActionsDesc class, can you add something like Debug.Log("Testing if we get here"); just to make sure the method is getting called. Is there any other information, any errors?

avatar image thornekey · Mar 13, 2014 at 10:56 AM 0
Share

ok, i tried that, absolutely no errors and the function isnt being called :S

avatar image stevethorne · Mar 13, 2014 at 01:26 PM 1
Share

Are you calling EatingFood() anywhere? You probably want it in your update loop

Show more comments
avatar image
0

Answer by Bunny83 · Mar 13, 2014 at 10:55 AM

It looks like you don't really understand class inheritance. If "InfoBox" is derived from MonoBehaviour then "playerActionsDesc" is also derived from MonoBehaviour. The complete inheritance chain looks like this:

 playerActionsDesc -> InfoBox -> MonoBehaviour -> Behaviour -> Component -> UnityEngine.Object -> System.Object

So every instance of "playerActionsDesc" is also a InfoBox as well as a MonoBehaviour as well as a Component ... as well as a System.Object.

If you have attached a InfoBox class to your player it has no relation to your "playerActionsDesc" class since "playerActionsDesc" is a subclass. You have to attach the playerActionsDesc class to your player. It serves the same things as your InfoBox class but also have this additional method.

Note: The information you provided is a bit thin. I based my answer on what i could read between the lines since you said InfoBox is attached to the player. Without more information we can't get more detailed here.

Also it's usually better to tell us what this is all about. What's the purpose of the class and from where do you want to call which method on which object for what reason?

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 thornekey · Mar 13, 2014 at 11:19 AM 0
Share

Ok more info:

The player has InfoBox, and doPlayerActions attached, as they are derived from mono.

playerActionsDesc doesnt derive from mono its derives from InfoBox ("public class playerActionsDesc : InfoBox {"). it is for the description of the action being done, i wanted to store all the descriptions in their own script so i can just call the function ins$$anonymous$$d of writing out a long message each time and making it look messy.

Now, the reason it derives from InfoBox is because of the function

         addGame$$anonymous$$essage("this is a description");

addGame$$anonymous$$essage is a funtion in InfoBox that sends a message to a dialogue box that can be used for NPC chat and of course, description. I have tested it and it works fine, by itself. Its just when i try to call it from another script..

here is an image to get an idea:

alt text

alt text

as you can see i can write anything i want.. and it will work.. inside the InfoBox script.. just have to make it work outside of it :P

picture 21.png (105.6 kB)
picture 18.png (15.1 kB)

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

23 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Making script object without declaring script name 2 Answers

Using classes between scripts in C#? 2 Answers

OnTriggerEnter On seperate object (than the script is attached to) 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