- Home /
How to call OnGUI() on class object
using UnityEngine;
using System.Collections;
public class Skill : MonoBehaviour {
private Texture txt;
private int number;
private int damage;
private int radius;
private int duration;
private float skillButWidth;
private float butX;
private float butY;
private Transform effect;
public bool renderBut;
public Skill(){
txt = null;
effect = null;
damage = 0;
radius = 0;
duration = 0;
number = 0;
skillButWidth = 0;
butX = 0;
butY = 0;
renderBut = false;
OnGUI();
}
#region skill Setters and Getters
public int Number{
get{ return number; }
set{ number = value; }
}
public Texture Txt{
get{ return txt; }
set{ txt = value; }
}
public int Damage{
get{ return damage; }
set{ damage = value; }
}
public int Radius{
get{ return radius; }
set{ radius = value; }
}
public int Duration{
get{ return duration; }
set{ duration = value; }
}
public Transform Effect{
get{ return effect; }
set{ effect = value; }
}
public float SkillButWidth{
get{ return skillButWidth; }
set{ skillButWidth = value; }
}
public float ButX{
get{ return butX; }
set{ butX = value; }
}
public float ButY{
get{ return butY; }
set{ butY = value; }
}
#endregion
public void render(bool r){
renderBut = r;
}
public void OnGUI(){
// if(renderBut){
Debug.Log("asd");
GUI.Box(new Rect(butX, butY, skillButWidth, skillButWidth), "");
//}
}
}
So i have this class. and this other script:
using UnityEngine;
using System.Collections;
public class ObjectTester : MonoBehaviour {
Skill[] skills;
public int numSkills;
private int width;
private int height;
private int butWidth;
void Start () {
numSkills = 18;
width = Screen.width;
height = Screen.height;
butWidth = width/20;
skills = new Skill[numSkills];
for(int i = 0; i<numSkills; i++){
skills[i] = new Skill();
}
for(int i = 0; i<numSkills; i++){
skills[i].SkillButWidth = butWidth;
}
for(int i = 0; i<numSkills; i++){
skills[i].ButX =butWidth/2 +(i*(butWidth)*1.5f)%(6*(butWidth)*1.5f);
if(i < 6){
skills[i].ButY =height - butWidth/2 - butWidth;
}
else if(i < 12){
skills[i].ButY =height - butWidth/2 - butWidth - (1*(butWidth)*1.1f);
}
else {
skills[i].ButY =height - butWidth/2 - butWidth - (2*(butWidth)*1.1f);
}
skills[i].render(true);
}
}
// Update is called once per frame
void Update () {
}
void OnGUI(){ //<>
}
}
As you can see i want to call the function render from the objecttester script on all the objects in skills[]. but the OnGui of the class Skill does not render any lables for the objects. It does not even print the "asd" that is in the OnGUI() of Skill class script. what am i doing wrong?
Thanks in advance!
Comment
Answer by DaveA · Oct 19, 2012 at 08:20 PM
You don't need this line:
OnGUI();
The 'on gui' function for Skill should not be OnGUI per-se, but what OnGUI would normally do. In fact, I would not inherit Skill from Monobehavior at all. It's just a structure really, right? So you could have a 'DoGUI' function like:
class Skill {
....
void DoGUI()
{
GUI.Box(new Rect(butX, butY, skillButWidth, skillButWidth), "");
}
}
Then your ObjectSetter class's OnGUI calls that like:
void OnGUI()
{
foreach (Skill skill in skills)
skill.DoGUI();
}