- Home /
Access variables,method from a Mono Behaviour without static use.
Hello i am currently trying to work on a univercity project which requires us to make a 3D game and i've been trying to write my own code so i can train more my coding and better understanding of how c# works. Currently i have found some solutions to my problems but they seem quite "heavy" on my Desktop.
So i would like to have some pointers on how to implement or fix my code further to do the following.
My problem is that all my code works if i make the variables i want as static but that would pose problems on the code i would write afterwards. For example.
We have this classes:
Char_Select : MonoBehaviour // this class requires mono behaviour because it has public gameobjects that need to be set in the inspector.
Base_Character //which doesn't require to inherit Monobehaviour since it only initialization of variables and some functions.
WizardSkills : MonoBehaviour // same as Char_Select
so far i was able to access methods and variables between those classes if i made static the variables and methods that needed to be accessed.Although this seems quite "wrong" and power consuming so i tried to do the following example.
Char_Select instance = new Char_Select();
then i tried accesing the variables in the Base_Character class but as it seems because Char_Select inherits from mono behaviour i would have to use the GetComponent but as far as i know if i do that the whole class will get added to my character for example a Wizard.
public class Char_Select : MonoBehaviour
{
public GameObject char1_glow;
public GameObject char2_glow;
public GameObject char3_glow;
public Transform ranger;
public Transform wizard;
public Transform warrior;
public bool isClicked = false;
public int ID = 0;
Lets say this is our variables for Char_Select class
public class Base_Character
{
public int Strength;
public int Agility;
public int Vitality;
public int Intelligence;
public int Wisdom;
private int level = 1;
public bool up = false;
private int xp;
private int xp_to_level;
public bool incombat = false;
And this is the Base_Character class
How would i access without static and without needing to add the Base_Character and the Char_Select class to the gameobject (for example the Wizard) from the following class?
public class WizardSkills : MonoBehaviour {
public GameObject force;
private bool shield = false;
private int reduction;
private GameObject target;
}
If you want to access a non static variable you need an instance of the Class. You can either assign this instance in the inspector or after instantiation, or in the case of components on the same object use GetComponent
at some point. I usually have some kind of scene manager script that has all the references I need.
so if i understand correctly i have to do something like a script that will [require[components]] of all my other scripts and create instances of my other code. i was thinking of making the functions and classes i want to connect virtual and then override them.Would that work? but i think i understand what you told me. Thanks a lot i will try to make it work. i hope if i have some other question i could get some more guidelines :)
$$anonymous$$aking the functions virtual wouldn't help at all. You're trying to access instance variables, not inherit behaviour. But you are correct in your earlier statement. That is how OO works :)