- Home /
Access a class on another script?
Hey guys I bump up a problem and I couldnt find an awnswer to ir anywhere so here I am asking for a little help.
So basically I have two scripts, on one I have a class, and on the other I want to create a fuction that will return type of that class of the other script, I've tried many different ways with no luck. I dont know if this is even possible but I apreciatte any help I could get, thank you in advance.
Script 1:
//Inventory.cs
public class Inventario : MonoBehaviour {
public class SlotInv {
public int[] ItemIndex;
public int[] ItemAmount;
}
public SlotInv Slot; //Testing
}
As you can see, here I have my class, and I made a variable from that so I could try to access the class from the other script, with no luck (through my logic)
Script 2:
//RecipeDataBase.cs
public class RecipeDataBase: MonoBehaviour {
public Inventory RecipeItems;
void Start () {
RecipeItems= transform.GetComponent<Inventory> ().Slot;
}
//Here's what I want to do (it's wrong but you can get the idea of what I'm trying to achieve)
public RecipeItems ReturnFinalItemIndex (int finalItemIndex) {
return 0;
}
}
This is just one of the many tries I made, this is just to illustrate what I want, so if anyone could help that would be awesome, or just say that is impossible to do. Thank You!
EDITED to give more info
If your one script defined a class called SlotInv
, you can't access it through any other name, other than SlotInv
. You might want to rename your class to Inventory
to match the rest of your code.
Your Script 2 is also not complete, it sort of looks like you're trying to write UnityScript in C#. You can't have variables and functions outside of a class.
For future reference, you should always post the exceptions you are getting so that we can give better responses. Right now I'm literally just guessing why your scripts aren't compiling.
Try this idea
private SlotInv slotScript;
public string x;
void Start()
{
slotScript = GameObject.Find ("Quad").GetComponent<SlotInv>();
x = slotScript.VariableName ;// accessing variable
slotScript.FunctionName();// accessing function
}
You can access public class features like
public functions
public void SampleFunction()// This function can access by another script { // some code } void XFunction()// This function cannot access by another script { // some code }
public static variables
public static bool isButtonPress1 ;// This variable can access by another script private bool isButtonPress2 ; // This variable cannot access by another script public bool isButtonPress3 ;// This variable cannot access by another script
Thank you for your answer, you idea doenst work. I can access variables if I want, my problem is to access a class (in this case inside the monobehaviour class), so a class inside another class, and then make a function that return a type of that class.
I was trying not to use the static variables because they can lead to bugs and at this point I had to change a lot of code...
Thank you for your comment, first to clarify some things (I should have posted on the question) both scripts inherit from monobehaviour. The only error I'm getting is this one:
Assets/Scripts/Inventory/RecipeDataBase.cs(55,16): error CS0118: RecipeDataBase.RecipeItems' is a
field' but a `type' was expected
Even if I change in the Start Function to
RecipeItems= transform.GetComponent<Inventory> ().SlotInv;
I get the same error
Hope this helps
I have seen that co$$anonymous$$g up previously and in the end a class inside works when the wrapping class is not $$anonymous$$onoBehaviour. At least I think it was going this way.
Why are not making your nested class a normal class and use it as you should:
public class SlotInv {
public int[] ItemIndex;
public int[] ItemAmount;
}
public class Inventario : $$anonymous$$onoBehaviour {
public SlotInv Slot; //Testing
}
If Inventario is not $$anonymous$$onoBehaviour then you can nest and use like
Inventario.SlotInv
but if a class is meant to be used outside its wrapping class then I don't see the point of it. You declare a class inside another class to use it as a container within the wrapper class, if you need to look into that container from somewhere else then make it easy on you and declare it outside.
Answer by Ermarrero · Apr 04, 2014 at 02:53 PM
I personally would use a generic list but you can use primitive array..Change to :
Debug.Log(ReturnFinalItemIndex(2));
public int ReturnFinalItemIndex (int finalItemIndex)
{
//Code for RecipeItems.ItemIndex or RecipeItems.ItemAmount;
return finalItemIndex;
}
that doesnt work for what I'm trying to do, but I've figured it out, and it was super simple that I feel stupid right now lol, I just had to pu my class outside the monobehaviour class and that way I am able to access it the way I want. Thank you guys for all the help.