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 /
  • Help Room /
avatar image
0
Question by TheAmerican · Jan 12, 2020 at 04:22 AM · scripting problemgameobjectprefabtextassets

Changing a Prefab's Text component seems to be broken.

Hi, I'm very new to Unity/programming, and for the life of me I can't figure out what's wrong here.

I wrote a script to generate a map (very basic one) of different terrain types.

One of the "terrains" is a city, that, when Instantiated, pulls a city name from a random list, and edits both the GameObject's name and a text box to have the name it selected.

When the script executes upon instantiation, sometimes the GameObject's name and the text box are different names, and sometimes the text box isn't changed at all (this seems to be random).

This script is attached to the prefab of the city, while the map creation script is elsewhere (and it works fine as far as I can tell, and is unrelated to changing the text).

Any ideas as to why this very simple script won't do what I need it to?

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 #if UNITY_EDITOR
     using UnityEditor;
 #endif
 using UnityEngine.UI;
 using System.IO;
 
 
 
 public class NameProcedure : MonoBehaviour
 {
     public TextAsset capitals; // Standard text file
 
     // Start is called before the first frame update
     void Start()
     {
         #if UNITY_EDITOR
             string path = AssetDatabase.GetAssetPath(capitals);
         #else 
             string path = (Application.dataPath + "/capitals.txt");
         #endif
 
         int fileLength = File.ReadAllLines(path).Length; // How long is the file?
         string[] names = File.ReadAllLines(path); // Each line is given to an array of city names.
 
         int rand = Random.Range(1, fileLength + 1); // Generate number between 1 and the length of the file (Random.Range(int, int) is max exclusive, hence the +1)).
         string chosenName = names[rand]; // Assign an string, chosenName, a randomly... chosen name.
 
         string cityName = GameObject.Find("CityName").GetComponent<Text>().text; // Find the original text information of the city name (from a Text component) for debug purposes.
 
         Debug.Log(chosenName); // Tell me what the random name is.
         Debug.Log(cityName); // Tell me what the original name is.
         if (GameObject.Find("CityName") == null) // If, for whatever reason, the "CityName" text component can't be found, log an error.
         {
             Debug.LogError("City not found!");
         }
         else
         {
             transform.name = chosenName; // The city prefab this script is attached to should now be named whatever name was chosen randomly.
             Debug.Log(transform.name); // Make sure this is true.
             GameObject.Find("CityName").GetComponent<Text>().text = chosenName; // Set the text of the city name to the new chosen name.
             Debug.Log(GameObject.Find("CityName").GetComponent<Text>().text); // Make sure it worked.
         }
     }
 }
 
Comment
Add comment · Show 1
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 Owen-Reynolds · Jan 12, 2020 at 05:09 AM 0
Share

$$anonymous$$aybe check for run time (red dot) errors. Those will "crash" the current script being run, skipping everything past, but otherwise it keeps running, so they're easy to miss. Also try testing GetComponent(Text).text by itself -- it should be fine. That should reassure you that the problem is somewhere else.

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by TheAmerican · Jan 12, 2020 at 06:29 AM

I fixed it by moving the script from the parent object (the city object) to the child I wanted to edit (the text box) and shortening the GetComponent parts (thanks to Owen Reynolds for the suggestion!).

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 #if UNITY_EDITOR
     using UnityEditor;
 #endif
 using UnityEngine.UI;
 using System.IO;
 
 
 
 public class NameProcedure : MonoBehaviour
 {
     public TextAsset capitals; // Standard text file
 
     // Start is called before the first frame update
     void Start()
     {
         #if UNITY_EDITOR
             string path = AssetDatabase.GetAssetPath(capitals);
         #else 
             string path = (Application.dataPath + "/capitals.txt");
         #endif
 
         int fileLength = File.ReadAllLines(path).Length; // How long is the file?
         string[] names = File.ReadAllLines(path); // Each line is given to an array of city names.
 
         int rand = Random.Range(1, fileLength + 1); // Generate number between 1 and the length of the file (Random.Range(int, int) is max exclusive, hence the +1)).
         string chosenName = names[rand]; // Assign an string, chosenName, a randomly... chosen name.
 
         string cityName = GetComponent<Text>().text; // Find the original text information of the city name (from a Text component) for debug purposes.
 
         Debug.Log(chosenName); // Tell me what the random name is.
         Debug.Log(cityName); // Tell me what the original name is.
         if (GameObject.Find("CityName") == null) // If, for whatever reason, the "CityName" text component can't be found, log an error.
         {
             Debug.LogError("City not found!");
         }
         else
         {
             transform.name = chosenName; // The city prefab this script is attached to should now be named whatever name was chosen randomly.
             Debug.Log(transform.name); // Make sure this is true.
             GetComponent<Text>().text = chosenName; // Set the text of the city name to the new chosen 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 Owen-Reynolds · Jan 12, 2020 at 08:15 AM 0
Share

Ah -- that last line with GetCompoennt all by itself was probably the problem. You can write code that always uses a1.transform, a2.GetComponent and so on, and it works fine. But in Unity you can also do things to "yourself" -- simply "transform" and "GetComponent". That can be confusing.

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

275 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Why does Unity lock the transform of prefabs in my scene in playmode? 0 Answers

Issue with Prefab instantiate and script not working? 0 Answers

How can I get gameObject from hirerchy to the public GameObject slot of a script attached to a prefab? 0 Answers

[Solved]Weird Time.timeScale behavior 3 Answers

Find Position of Destroying Object ? 1 Answer


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