- Home /
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?
I'm not sure if this is the variable you want. Try gameObject.GetComponent < $$anonymous$$yClass > ().$$anonymous$$yFunction().
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.
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();
}
}
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?
ok, i tried that, absolutely no errors and the function isnt being called :S
Are you calling EatingFood() anywhere? You probably want it in your update loop
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?
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:
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
Your answer
Follow this Question
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