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 ReggieBeRetro · Jul 08, 2016 at 11:21 AM · scripting problemobjecttextfindrename

Cant find text objects after being renamed.

Hello

So i had these two scripts working until i figured out each object has to have a different name for the ObjectTextDisplay script to work correctly. SO i updated the randomize script to give unique names to each object that is created. Now when it comes to updating the ObjectDisplayText script i am gettin errors because the name that the object was initially created with no longer exists. In the Randomize script you can see how they are renamed. My problem starts on line 24 of the ObjectDisplayText script.

ObjText = GameObject.Find ("ObjectText").GetComponent ();

should be somthing like this x being the number of the object in its parented hierarchy

ObjText = GameObject.Find (gameObject.name + "_Obj" + x + "_ObjectText").GetComponent (); ObjText2 = GameObject.Find (gameObject.name + "_Obj" + x + "_ObjectText2").GetComponent ();

Can anyone shed some light on the subject. I hope thats not to convoluted.

Randomize Objects File

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine.UI;
 
 public class RandomizeObj : MonoBehaviour {
     public List<Transform> ObjList = new List<Transform>();
     //public Transform obj;
     public Vector3 postiion;
     public GameObject objspawned;
     public int totalobjs;
     private int ObjNum;
     public float ObjectsetWidth;
     public float ObjectsetLength;
     public float respawntime = 3600.0f;
     public float respawn = 3600.0f;
     private Text ObjText;
     private Text ObjText2;
 
     //respawn object if destryed after 60minutes - 3600 Seconds - 3600000.0f milliseconds
 
 
     void Start () {
 
         int spawned = 0;
 
         while (spawned < totalobjs) {
             Vector3 position = new Vector3 (transform.position.x + Random.value * ObjectsetWidth, transform.position.y, transform.position.z + Random.value * ObjectsetLength);
 
             RaycastHit hit = new RaycastHit ();
 
             if (Physics.Raycast (position, Vector3.down, out hit)) {
                 ObjNum = Random.Range (0,ObjList.Count);
                 if ((hit.collider.gameObject.tag != "Tree") && (hit.collider.gameObject.tag != "Stone") && (hit.collider.gameObject.tag != "Bush") && (hit.collider.gameObject.tag == "Roads") && (hit.collider.gameObject.tag != "Debris") && (hit.collider.gameObject.tag != "Pickup")) {
 
                     Transform newObj = (Transform)Instantiate (ObjList[ObjNum], hit.point, Quaternion.identity);
                     newObj.transform.SetParent (transform);
 
                     newObj.name = gameObject.name + "_Obj" + spawned;
                     ObjText = GameObject.Find ("ObjectText").GetComponent<Text> ();
                     ObjText2 = GameObject.Find ("ObjectText2").GetComponent<Text> ();
 
                     ObjText.name = gameObject.name  + "_Obj" + spawned  + "_ObjectText";
                     ObjText2.name = gameObject.name  + "_Obj" + spawned + "_ObjectText2";
 
                 } else {
                     while ((hit.collider.gameObject.tag == "Tree") && (hit.collider.gameObject.tag == "Stone") && (hit.collider.gameObject.tag == "Bush") && (hit.collider.gameObject.tag == "Roads") && (hit.collider.gameObject.tag == "Debris") && (hit.collider.gameObject.tag == "Pickup")) {
                         position = new Vector3 (transform.position.x + Random.value * ObjectsetWidth, transform.position.y, transform.position.z + Random.value * ObjectsetLength);
 
                         hit = new RaycastHit ();
                     }
                     if (Physics.Raycast (position, Vector3.down, out hit)) {
                         Transform newObj = (Transform)Instantiate (ObjList[ObjNum], hit.point, Quaternion.identity);
                         newObj.transform.SetParent (transform);
 
                         newObj.name = gameObject.name + "_Obj" + spawned;
 
                         ObjText = GameObject.Find ("ObjectText").GetComponent<Text> ();
                         ObjText2 = GameObject.Find ("ObjectText2").GetComponent<Text> ();
                     
                         ObjText.name = gameObject.name  + "_Obj" + spawned + "_ObjectText";
                         ObjText2.name = gameObject.name  + "_Obj" + spawned + "_ObjectText2";
 
                         //Debug.Log (hit.collider.gameObject.tag + " " + newObj.name + " Landed on top of object");
                     }
                 
 
                 }
 
             }
             spawned++;
         }
 
 
 
     }
     void Update() {
         respawntime -= Time.deltaTime;
         if (respawntime < 0) {
             CheckForObjs ();
         }
     }
     void CheckForObjs() {
         int i = 0;
         while (i < totalobjs) {
 
 
 
             if (objspawned == null) {
                 Vector3 position = new Vector3 (transform.position.x + Random.value * ObjectsetWidth, transform.position.y, transform.position.z + Random.value * ObjectsetLength);
                 ObjNum = Random.Range (1,ObjList.Count);
                 objspawned = GameObject.Find(gameObject.name + "_Obj" + i);
                 RaycastHit hit = new RaycastHit ();
                 if (Physics.Raycast (position, Vector3.down, out hit)) {
                     Transform newObj = (Transform)Instantiate (ObjList[ObjNum], hit.point, Quaternion.identity);
                     newObj.transform.SetParent (transform);
 
                     newObj.name = gameObject.name + "_Obj" + i;
                     Debug.Log ("Not Found Object at " + i);
                 } 
             }
 
             i++;
         }
         respawntime = respawn;
 
     }
 }
 

Object Text Display Script

 using UnityEngine;
 using UnityEngine.UI;
 
 using System.Collections;
 
 
 public class ObjectTextDisplay : MonoBehaviour {
 
     public string ObjString;
     public string ObjString2;
     public Text ObjText;
     public Text ObjText2;
     public float fadeTime;
     public float fadeTime2;
     public bool displayInfo;
     public Vector3 objfwd;
     int rayLength = 3;
     // Use this for initialization
     void Start () {
         
 
 
 
         ObjText = GameObject.Find ("ObjectText").GetComponent<Text> ();
         ObjText.color = Color.clear;
         ObjText2 = GameObject.Find ("ObjectText2").GetComponent<Text> ();
         ObjText2.color = Color.clear;
         displayInfo = false;
     }
     
     // Update is called once per frame
 
     void  Update (){
         
 
             FadeText ();
 
         if(Input.GetKeyDown (KeyCode.Escape)){
 
             Screen.lockCursor = false;
 
         }
     }
 
 
     void FadeText() {
         
 
         if (displayInfo) {
             ObjText.text = ObjString;
             ObjText.color = Color.Lerp (ObjText.color, Color.white, fadeTime * Time.deltaTime);
             ObjText2.text = ObjString2;
             ObjText2.color = Color.Lerp (ObjText.color, Color.white, fadeTime2 * Time.deltaTime);
 
         } else {
             
             StartCoroutine (ObjCoroutine());
         }
 
 
     }
     IEnumerator ObjCoroutine()
     {
         //This is a coroutine
 
 
         yield return new WaitForSeconds (20f);    //Wait one frame
 
     
 
             ObjText.color = Color.Lerp (ObjText.color, Color.clear, fadeTime * Time.deltaTime);
             ObjText2.color = Color.Lerp (ObjText.color, Color.clear, fadeTime2 * Time.deltaTime);
     
         displayInfo = false;
 
     }
 }
 

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
Best Answer

Answer by ReggieBeRetro · Jul 08, 2016 at 01:46 PM

Got it!.. Answered my own question.. XD only took 3 hours

 ObjText = GameObject.Find (gameObject.transform.GetChild(0).GetChild(0).gameObject.name).GetComponent<Text> ();
         ObjText.color = Color.clear;
         ObjText2 = GameObject.Find (gameObject.transform.GetChild(0).GetChild(1).gameObject.name).GetComponent<Text> ();
         ObjText2.color = Color.clear;

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

72 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

Related Questions

Mysterious Extra KeyUp Event (5.3.3f1) 2 Answers

Accessing UI text 0 Answers

How do you change UI Text to an int? 4 Answers

NullReferenceException on Text element 1 Answer

How can I use a scribt two combine two objects on collision with fixedjoint? 0 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