Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 ThePak75 · Nov 28, 2016 at 08:04 AM · gameobjectinstantiateruntimenested

How to display my castle name at runtime

I am trying to create a "Window" with unity's UI system. alt text When I click a "Castle" tile I want to see the window and it's TitleBar display some text.

It has a bunch of nested GameObjects. alt text

There are two parts to my code, A CastleScript which handles the click and instantiates a new window. And a script attached to the window which will let me set the text (to anything other than "New Text".

Town Window Script :

 using UnityEngine;
 using System.Collections;
 using UnityEngine.UI;
 
 public class TownWindowScript : MonoBehaviour {
 
     public Transform WinPrefab;
     public Transform TownWindow;
     
     // Use this for initialization
     void Start () {
         
     }
     
     // Update is called once per frame
     void Update () {
     
     }
 
     void OnMouseDown()
     {
         string s;
         s = "";
         Debug.Log("TownWindowScript.cs OnMouseDown() (This will instantiate)");
         TownWindow = Instantiate(WinPrefab);
 
         TownWindow.tag = "Window-Town";
         //TownWindow.GetComponent<WindowScript>().WindowTitle = GetComponent<CastleScript>().TownName;
         s = GetComponent<CastleScript>().TownName;
         TownWindow.GetComponent<WindowScript>().SetWindowTitle(s);
         Debug.Log("Done");
         //Transform t = TownWindow.transform.Find("TitleBarText");
         //Text tt = t.GetComponent<Text>();
         //tt.text = "asdf";//GetComponent<CastleScript>().TownName;
     }
 
 }
 

And the Window Script:

 using UnityEngine;
 using System.Collections;
 using UnityEngine.UI;
 
 public class WindowScript : MonoBehaviour {
 
     private Transform BackPanel;
     private Transform FloatingWindow;
     public string WindowTitle;
 
     //Used for initializeation after all (sub?) objects have been instantiated
     void Awake()
     {
         Debug.Log("WindowScript.cs Awake()");
         if (tag == "Window-Town")
         {
             Debug.Log("Here");
             //setup Window Tile Text
             if (WindowTitle == "") { WindowTitle = "New Window"; }
             else
             {
                 Transform t = transform.Find("WindowTitle");
                 t.GetComponent<Text>().text = WindowTitle;
             }
         }
     }
     // Use this for initialization
     void Start () {
         Debug.Log("WindowScript.cs Start()");
         //Get Window Segments
         BackPanel = transform.Find("BackPanel");
         FloatingWindow = transform.Find("FloatingWindowPanel");
 
         //Disable segments by type
         if (tag == "Window-Town")
         {
             //Don't use back panel for town
             BackPanel.gameObject.SetActive(false);
             GameObject[] go = GameObject.FindGameObjectsWithTag("Window-Town");
 
             //Only allow one Town Window
             if (go.Length > 1 ) { Destroy(transform.root.gameObject); }
         }
         else if (tag == "Window-Menu")
         {
             FloatingWindow.gameObject.SetActive(false);
         }
     }
     
     // Update is called once per frame
     void Update () {
     
     }
 
     public void SetWindowTitle(string s)
     {
         Transform t;
         t = transform.Find("TitleBarText");
         t.GetComponent<Text>().text = s;
     }
 }
 

When I click the castle I get a window, but the text is the default "New Text" not my dynamic castle name. The Console displays:

TownWindowScript.cs OnMouseDown (This will instantiate)

WindowScript.cs Awake()

Null Reference Exception (WindowScript.cs Line 59)

WindowScript.cs Start()

I expect that my child objects are not created when I call SetWindowTitle. Does anyone have suggestions or work arounds for this?

windowandcastle.png (8.2 kB)
hierarchy.png (9.3 kB)
Comment
Add comment · Show 4
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 ThePak75 · Nov 28, 2016 at 07:52 PM 0
Share

Any takers on this one?

avatar image hexagonius · Nov 28, 2016 at 08:17 PM 0
Share

i guess you have that script on the root gameobject? Find does not check the role hierarchy tree, you need to build a whole string for the hierarchy chain (like "root/next/next/yey").
a better way would be to shortcut this with a Text variable you assign the relevant Text to

avatar image ThePak75 hexagonius · Nov 28, 2016 at 08:48 PM 0
Share

Hmmm, I may have misread how that works. I can't wait to get off work to try this out!

avatar image ThePak75 hexagonius · Nov 29, 2016 at 04:55 PM 0
Share

This was the answer! Thank you.

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by callme · Nov 28, 2016 at 09:03 PM

try this way public Text title; // assign this in inspector and then just title.text = name

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 ThePak75 · Nov 29, 2016 at 04:56 PM 0
Share

I have a hierarchy of about 10 game objects.

Otherwise this would work.

avatar image
0

Answer by Chrisasan · Nov 29, 2016 at 06:52 PM

I believe I know what might be happening to your text string.

First thing that happens is your window is created, this creates the text string. Next step is your assigning the window the text you want, that does happen.

Now after you have assigned the title text your text, the awake function for the window is called and replaces that new text with the default text.

I believe that is what is happening, because in your source code you have set the title to "", and when it sees "" it replaces that with "New Window".

The awake() function is called after you have assigned "" to the window's title text string.

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 Chrisasan · Nov 29, 2016 at 06:54 PM 0
Share

The Start() function and I think the Awak() function come after your game loop is complete.

avatar image
0

Answer by Whiteleaf · Nov 29, 2016 at 08:25 PM

There is a null reference where this happens:

 t.GetComponent<Text>().text = s;

Which leads me to believe "t" does not have a Text component attached to it. Or, "t" is completely null because it can't find an object with the name "TitleBarText".

Make sure the "TitleBarText" is actually a parent of the transform. Because transform.Find searches for any child of x transform with the given name.

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

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

8 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Instantiate object 1 Answer

Referencing instantiated objects at runtime 2 Answers

problems parenting a gameObject to another upon instantiating when same name exists 1 Answer

Can I use AddComponent to add (this.script) to an object? 1 Answer

Spawn items around GameObject 3 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