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 jetty112 · Oct 19, 2012 at 06:30 PM · objectfunctionclassrenderongui

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
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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();
   }

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 jetty112 · Oct 20, 2012 at 11:44 AM 0
Share

That worked perfectly. Thanks alot! :)

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

10 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

Related Questions

Inherited class, trying to overwrite variable. 0 Answers

GUI set max amount of characters for Label 1 Answer

Obj calls function, on another obj and get's destroyed. Function stops 1 Answer

Getting any type in Unity Inspector 1 Answer

How do I Create 5 Random Racers? 2 Answers


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