Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by Fornoreason1000 · Jul 12, 2014 at 11:08 AM · guitransformtextparent3dtext

Why is setting the parent of a text transform alters its world coordinates?

Hi eveyone,

I seem to have found something strange with the transform.parent function... it is seeming to contradict what it says in the docs "modify the parent-relative position, scale and rotation but keep the world space position, rotation and scale the same.". Usually this is the case... but in this particular case this does not occur

http://docs.unity3d.com/ScriptReference/Transform-parent.html

My Goal

I'm currently making a function to generate a text for GUI purposes the function is called DrawText and returns a TextMesh object. the aim of this is so whenever i need to add text in game on a GUIBox or something i can call this function and place it where i need to in ScreenSpace... becuase for the most part this will be done within a Custom GUI Object thus the text should be parented to the GUI Object. the Draw Text function is as below

    public TextMesh DrawText(Rect rect, string text)
     {
         GameObject textGo = new GameObject();
         textGo.name = "text";
         textGo.layer = LayerMask.NameToLayer("UI");
         Debug.Log(viewport);
 
         float x = rect.x + this.rect.x;
         float y = Screen.height - (rect.y + this.rect.y);
        
 
 
         textGo.transform.position = (viewport.camera.ScreenToWorldPoint(new Vector3(x, y, 0.99f)));
         textGo.transform.parent = gameObject.transform;
     
      
         
         this.text = textGo.AddComponent("TextMesh")  as TextMesh;
         this.text.font = Resources.Load("GUI/Font/MainFont") as Font;
         this.text.richText = true;
         this.text.renderer.material = this.text.font.material;
         this.text.characterSize = 0.1f;
         this.text.fontSize = 24;
         this.text.text = text;
         return this.text;
 
     }

view port is the camera object I use to render GUI on a separate layer.. an inbuilt layer called UI. it is orthographic with a size of 5 usually at the worlds origin (0,0,0).

rect refers to where the text will be placed on the GUI object this.rect is the pixel coordinates of the GUIObject not the text itself

The Problem

now the problem with this code is in this where i set the parent of the text object to the GUIobject transform... when that line is called the to object is shifted to a very strange position... it seems to start ignoring the fact that i moved the object with the gameObject resulting it moving it out of the screen ... its local coordinates seem equal to the values of the parent + itself.... e.g(Box would read -8.8x 5y in world space , text would read -8.68x 4.88y in local space)

My attempt on fixing it

so incidentally i tried removing this.rect from the equation. this did not solve thew issue ... it didn't change a thing.... I have also tried adding the transform to the text since with this
textGo.transform.position = (viewport.camera.ScreenToWorldPoint(new Vector3(x, y, 0.99f))) + transform.position;

that just caused it to completely shift out of line. with or without considering this.rect.

now this is where it just gets weird.... by removing the transform.parent code... everything will line up perfectly... also this seems to only ever ever happen when the transform.parent code is called from within or anything that originates from Start()... infact if a i call the transform.parent code in the very next frame... it will work...perfectly it only seems to affect it within start(). in the screen shots provided you noticed HAHAHA is only affected... as it is called within start()

alt text

here is the code where DrawText is called

 public class GUITitleInterface : GUIInterface {
 
 
     GUI3DWindow win;
     WindowTitleCommand select;
     TextMesh text;
     // Use this for initialization
     public override void Start()
     {
          win =  GUI3DWindow.New(new Rect(0, 0, Screen.width - 20, 100));
          select = WindowTitleCommand.New(new Rect(400, 300, 200, 200));
         ResourceManager.PlayLoopingClipAtPoint(Resources.Load("Audio/BGM/Prelude") as AudioClip, Vector3.zero);
         win.DrawText(new Rect(10, 10, 0, 0), "HAHAHAHA!");
       
     }

now i know i could just call the code elsewhere without any major performance issues but what what i want to know is why?

transform parenting 1.jpg (57.3 kB)
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

0 Replies

· Add your reply
  • Sort: 

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

21 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

Related Questions

Making text look clear 2 Answers

Changing an objects transform position 1 Answer

Display Name Above Object 2 Answers

I want pixelated unicode text. Need suggestions... 2 Answers

Detect Text in GUI; Print 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