Author Topic: [Solved] Referencing UILabel values from GameObject's on a UIGrid UIScrollView  (Read 3681 times)

rahares

  • Newbie
  • *
  • Thank You
  • -Given: 1
  • -Receive: 0
  • Posts: 3
    • View Profile
Alright with everything you have said I finally understand how to make this work.

Thank you very much.  Sorry if my questions sounded stupid but as you've pointed out I'm noob.

For anyone doing the same its actually simple HA!  Just like he said, but I dumbed it way down for us noobs! HA HA!

-----------------------------------------------------------------------------------------------------------
Usage: Add this script to the Prefab that will get added to your UIGrid/UIScrollView.  Whenever this object is clicked it will set the values in the other script that needs to access to these values.

Script: ObjectOnClick.cs
----------------------------------------------------------------------------------------------------------

using UnityEngine;
using System.Collections;

public class ObjectOnClick : MonoBehaviour {

   public UILabel theLabel1; // assign this from the inspector using the UILabel in the prefab
   public UILabel theLabel2; // repeat for however many you need to access
       
        // This runs whenever the object gets clicked on
    void OnClick () {
                // This will access whatever the gameobject is that has the script
                // It will then assign the values from this object to those of the other script
                GameObject target = GameObject.Find("GameObjectName which has the script/values to access");
                SCRIPTNAME script = target.GetComponent<SCRIPTNAME>();
                script.MYVALUE = theLabel1.text;
                script.MYVALUE2 = theLabel2.text;
   }
}
« Last Edit: May 20, 2014, 08:55:26 PM by rahares »

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: UIGrid UIScrollView Getting the values of the gameobject labels
« Reply #1 on: May 18, 2014, 04:43:39 PM »
GetComponentInChildren gets you 1 item, yet you are getting it as "UILabel[]"? That's a compile error.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: UIGrid UIScrollView Getting the values of the gameobject labels
« Reply #2 on: May 18, 2014, 11:25:10 PM »
This is a support forum for NGUI. What you're asking is basic C# / Unity. You need to use GetComponentsInChildren. Check Unity's documentation.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: UIGrid UIScrollView Getting the values of the gameobject labels
« Reply #3 on: May 19, 2014, 05:41:51 PM »
A prefab can only reference objects that are a part of the prefab. A far a the prefab is concerned, objects outside of it don't exist at all, because a prefab is not a part of the scene.

A prefab instance can reference objects outside of it because it's a part of the scene. This is just how Unity works, and makes perfect sense if you think about it.

UICenterOnChild.selectedObject tells you the "selected" object (technically it's just an object in the center, not a selected object).

UICamera.selectedObject tells you the actual selected object, of which there is only one in the scene at any given time.

I'm not sure what NGUITools.GetChild is supposed to be. What is passing a game object supposed to do in that? You can access list of children like any other game object -- via transform.GetChild. There isn't just one child. There can be 0 or more children. If you need to find something like a label inside a prefab, consider either using transform.FindChild("name of game object").GetComponent<UILabel>(), or preferably creating a helper script that has public references to your objects like labels, sprites etc, placed on the prefab itself:
  1. using UnityEngine;
  2.  
  3. public class MyClass : MonoBehaviour
  4. {
  5.     public UILabel title;
  6.     public UILabel content;
  7. }
Once you have this script attached to the prefab you can set the values in inspector. Changing the label's text at that point becomes as simple as:
  1. GameObject go = NGUITools.AddChild(gameObject, prefab);
  2. MyClass script = go.GetComponent<MyClass>();
  3. script.title.text = "New Title";
  4. script.content.text = "New Content";

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Why do you keep insisting on trying to find objects when I showed you how you can reference them?
Quote
Static member `UICamera.selectedObject' cannot be accessed with an instance reference, qualify it with a type name instead.
This tells you the error. You are trying to do camera.selectedObject, and 'selectedObject' is a static property. You access it literally like this:
  1. Debug.Log(UICamera.selectedObject.name);
You need to get more familiar with C#.