Author Topic: Creating Buttons  (Read 13406 times)

kwelts

  • Guest
Creating Buttons
« on: June 25, 2012, 11:55:24 AM »
I have the following code:

using UnityEngine;
using System.Collections;

public class ObjectArray : MonoBehaviour {
   
   object uiObject;
   
   public void main() {
   string[] array1 = new string[8] {"Do", "Re", "Mi", "Fa", "So", "La", "Ti", "Doe"};   
      int dx = 40;
      int dy = 40;
      uiObject = GameObject.Find("Button");
   }
      
   public void createButtons() {
         
      for (int i = 0; i < 8; i++) {
            NGUITools.AddChild(uiObject);
            // uiObject.titleLabel.Font = [UIFont systemFontOfSize:size];
            uiObject.TitleLabel.Text = array1;
            uiObject.Transform.Translate(Vector3.right * dx, Vector3.down * dy);
         } // end for()
   }
}

I am trying to make buttons for each index of an array. my code no longer has red underlines but the compiler says that there are invalid arguments for "NGUITools.AddChild(uiObject)". How do I fix this?

PhilipC

  • Guest
Re: Creating Buttons
« Reply #1 on: June 25, 2012, 12:02:50 PM »
Your trying to pass a object of type 'object' the function is expecting 'GameObject' the compiler will not automatically cast this for you.

Ferazel

  • Full Member
  • ***
  • Thank You
  • -Given: 1
  • -Receive: 2
  • Posts: 150
    • View Profile
Re: Creating Buttons
« Reply #2 on: June 25, 2012, 12:10:05 PM »
Phillip got here first, but the problem is that object != GameObject so the compiler is warning you about that.

Also if you want to create duplicate instances of a button that you setup in the editor you are still going to need to perform a GameObject.Instantiate(uiObject); before you do a NGUITools.AddChild().

  1. ...
  2. GameObject newButton = GameObject.Instantiate(uiObject);
  3. NGUITools.AddChild(newButton);
  4. UILabel newButtonLabel = newButton.GetComponentInChildren<UILabel>();
  5. newButtonComp.text = array1[i];
  6. ...
  7.  

kwelts

  • Guest
Re: Creating Buttons
« Reply #3 on: June 25, 2012, 01:56:44 PM »
Forgive me as i'm new to C#. I have put in your changes and changed the code a tiny bit more but it still gives an error "Cannot implicitly convert type object to GameObject. an explicit conversion exists (are you missing a cast?)". I cant see the error in what I did. thanks in advance:

using UnityEngine;
using System.Collections;

public class ObjectArray : MonoBehaviour {
   
   UIButton uiObject;
   
   public void main() {
   string[] array1 = new string[8] {"Do", "Re", "Mi", "Fa", "So", "La", "Ti", "Doe"};   
      int dx = 40;
      int dy = 40;
      uiObject = GameObject.Find("Button").GetComponent<UIButton>();
   }
      
   public void createButtons() {
         
      for (int i = 0; i < 8; i++) {
            GameObject newButton = GameObject.Instantiate(uiObject);
            NGUITools.AddChild(newButton);
            UILabel newButtonLabel = newButton.GetComponentInChildren<UILabel>();
            newButtonComp.text = array1;
            uiObject.Transform.Translate(Vector3.right * dx, Vector3.down * dy);
         } // end for()
   }
}

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Creating Buttons
« Reply #4 on: June 25, 2012, 02:02:19 PM »
GameObject newButton = GameObject.Instantiate(uiObject) as GameObject;

However, I suggest doing this instead:

  1. GameObject newButton = NGUITools.AddChild(gameObject, uiObject);

kwelts

  • Guest
Re: Creating Buttons
« Reply #5 on: June 25, 2012, 02:23:33 PM »
"(gameObject, uiObject);"

this part doesnt work. it expects the uiObject but gets the gameObject instead.

Nevermind I got this part working. will post if another error arises.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Creating Buttons
« Reply #6 on: June 25, 2012, 02:25:25 PM »
It needs to be a prefab. Change "UIButton uiObject;" to "GameObject uiObject;"

I suggest you learn how to work with prefabs in Unity.

kwelts

  • Guest
Re: Creating Buttons
« Reply #7 on: June 26, 2012, 10:30:23 AM »
I'm now getting a nullReferenceException:

using UnityEngine;
using System.Collections;

public class ObjectArray : MonoBehaviour {
   
   GameObject uiObject;
   string[] array1;
   Vector3 temp = new Vector3(0,50.0f,0);
   
   void Start() {
   array1 = new string[8] {"Do", "Re", "Mi", "Fa", "So", "La", "Ti", "Doe"};
      /*for(int i=0; i < array1.Length; i++) {
      Debug.Log(array1);
      } */
      createButtons();
      uiObject = GameObject.Find("Button");
   }
      
   public void createButtons() {
      //Debug.Log("Got here!");
      for (int i = 0; i < array1.Length; i++) {
         GameObject newButton = GameObject.Instantiate(uiObject) as GameObject;
            NGUITools.AddChild(newButton);
         UILabel newButtonLabel = newButton.GetComponentInChildren<UILabel>();
         newButtonLabel.text = array1;
         newButton.transform.position += temp;
         } // end for()
   
   } // end createButtons()
   
} // end class()

I've created a prefab of a button and I need to instantiate it in the scene but think the error might be because I do not know how to find the prefab since GameObject is only finding items in the hierarchy and not the project view itself.

PhilipC

  • Guest
Re: Creating Buttons
« Reply #8 on: June 26, 2012, 10:58:05 AM »
As suggested by Mike earlier.

  1. GameObject newButton = NGUITools.AddChild(gameObject, uiObject);

Should be used instead of

  1. GameObject newButton = GameObject.Instantiate(uiObject) as GameObject;
  2. NGUITools.AddChild(newButton);
  3.  

You should create the prefab but also make GameObject uiObject public so you can assign the prefab to the variable from the inspector.

The reason you are getting a Null Referenceexception because you currently have

  1. createButtons();
  2. uiObject = GameObject.Find("Button");
  3.  

but you are trying to use uiObject inside of createButtons() (aka you have not assigned uiObject yet).

kwelts

  • Guest
Re: Creating Buttons
« Reply #9 on: June 26, 2012, 11:13:46 AM »
thank you very much for the response. Ive made the changes but now have noticed that when i hit play, it clears my uiObject which is why im getting a nullReferenceException. pics included of before pressing play, and while scene is running:


PhilipC

  • Guest
Re: Creating Buttons
« Reply #10 on: June 26, 2012, 11:55:23 AM »
Do you still have the uiObject = GameObject.Find("Button"); in your code? if so remove it as its not longer needed

kwelts

  • Guest
Re: Creating Buttons
« Reply #11 on: June 26, 2012, 12:04:40 PM »
thank you very much! I'll look into why it works that way.

kwelts

  • Guest
Re: Creating Buttons
« Reply #12 on: July 03, 2012, 01:35:59 PM »
Hello again. So i've gotten the create buttons to work properly when its an isolated class, but now I am trying to call it from a static method:


  1. //Method
  2.  
  3. public static void AddCheckInButtons(){
  4.  
  5. for(int i = 0; i < checkInArray.Count; i++){
  6.  
  7. Debug.Log("--->Start For Loop");
  8.  
  9. GameObject g = GameObject.Find("CheckIn_Panel");
  10.  
  11. Debug.Log("--->GameObject");
  12.  
  13. CheckInButton checkButton = g.AddComponent<CheckInButton>();
  14.  
  15. Debug.Log("--->CheckIn Button");
  16.  
  17. checkButton.SetPosition(i);
  18.  
  19. checkButton.SetCheckInObject(checkInArray[i]);
  20.  
  21. checkButton.CreateButton();
  22.  
  23. }
  24.  
  25. }
  26.  
  27.  
  28.  
  29. //Class
  30.  
  31. using UnityEngine;
  32.  
  33. using System.Collections;
  34.  
  35.  
  36.  
  37. public class CheckInButton : MonoBehaviour {
  38.  
  39. //Properties
  40.  
  41. string ButtonName {get;set;}
  42.  
  43. int Position{get;set;}
  44.  
  45. CheckIn CheckInObject {get;set;}
  46.  
  47.  
  48. //Variables
  49.  
  50. public GameObject uiObject;
  51.  
  52. float buttonVariance = -45.0f;
  53.  
  54.  
  55. void Start(){}
  56.  
  57.  
  58. //Creates a button to use with Check In UI
  59.  
  60. public void CreateButton(){
  61.  
  62. Debug.Log("--->CreateButton");
  63.  
  64. GameObject newButton = NGUITools.AddChild(gameObject, uiObject);
  65.  
  66. Debug.Log("--->GameObject newButton");
  67.  
  68. UILabel newButtonLabel = newButton.GetComponentInChildren<UILabel>();
  69.  
  70. Debug.Log("--->UILabel newButtonLabel");
  71.  
  72. newButtonLabel.text = ButtonName;
  73.  
  74. newButton.transform.localPosition = new Vector3(0.0f, ((buttonVariance*Position)+140.0f), 0.0f);
  75.  
  76. }
  77.  
  78.  
  79. //Setter for Position
  80.  
  81. public void SetPosition(int position){
  82.  
  83. Position = position;
  84.  
  85. }
  86.  
  87.  
  88. //Setter for CheckInObject and ButtonName
  89.  
  90. public void SetCheckInObject(CheckIn checkIn){
  91.  
  92. CheckInObject = checkIn;
  93.  
  94. ButtonName = checkIn.name;
  95.  
  96.  
  97. }
  98.  
  99. }

from running traces it seems to not be working at:
"GameObject newButton = NGUITools.AddChild(gameObject, uiObject);"
it is giving me a null exception.

Is there a way to determine which part of the line is null? As far as I can tell all the material is in the proper places in prefabs/hierarchy.

PhilipC

  • Guest
Re: Creating Buttons
« Reply #13 on: July 03, 2012, 02:46:09 PM »
Not being able to view you scene i can only take a guess at whats wrong by the code.

Your doing a "CheckInButton checkButton = g.AddComponent<CheckInButton>();" which suggests to me that your are adding the script dynamically to the "CheckIn_Panel". If so "public GameObject uiObject;" will not be set as you don't set it anywhere. This would cause the null reference exception.

Also as a side note why are you adding the CheckInButton component to the same GameObject (which you are also finding each iteration) multiple times (talk about a performance hit!)?