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 fabricius_2 · Sep 21, 2015 at 09:33 AM · c#positioncanvasscripting beginnerrecttransform

Change position of a text prefab added to a world space canvas via scripting

I'm building a scene where I want to render numbers 1-100 on the "floor" of the world space. I want to add them via scripting and position them in a grid. For now, I'm just rendering a single number which I'm trying to change the position of.

I'm able to instantiate the number (a Text component) from a prefab, set my Canvas as the parent to get it rendered in the right initial place and change the text. So far so good. But when I try change the position of the number, nothing happens. I've tried all the different ways of setting the position I could think of, but the number just stays in the initial corner keeping the position of the prefab.

I've also tried debugging in Visual Studio, and it seems like my calls to ie. anchoredPosition.Set() have no effect when I look at the position before and after the call via the debugger.

I'm a complete newbie and this might very well be something very simple I'm missing. Possibly canvas just work differently in world space than in the screen space. Any ideas?

 using UnityEngine;
 using UnityEngine.UI;
 
 public class HundredBoard : MonoBehaviour {
 
   public GameObject Number;
 
   // Use this for initialization
   void Start () {
     this.generateBoard();
     }
     
     // Update is called once per frame
     void Update () {
     //this.generateBoard(); // Also tried this. It generates many numbers, but all in the same position.
   }
 
   void generateBoard() {
 
     // These first lines works, number is instantiated, inserted and changed from "1" to "2"
     var number = Instantiate<GameObject>(Number);
     number.transform.SetParent(transform, false);
     number.GetComponent<Text>().text = "2";
 
     var ntransform = number.GetComponent<RectTransform>();
 
     // None of these lines seem to have any effect
     ntransform.offsetMax.Set(5, 5);
     ntransform.offsetMin.Set(3, 3);
     ntransform.anchoredPosition.Set(15, -23);
     ntransform.anchoredPosition3D.Set(15, -23, 0);
 
     number.GetComponent<Text>().rectTransform.position.Set(10, 0.15f, 0);
     number.GetComponent<Text>().rectTransform.localPosition.Set(10, 0.15f, 0);
     number.GetComponent<Text>().transform.position.Set(10, 0.15f, 0);
     number.GetComponent<Text>().transform.localPosition.Set(10, 0.15f, 0);
     number.GetComponent<Text>().rectTransform.anchoredPosition.Set(15, -23);
     number.GetComponent<Text>().rectTransform.anchoredPosition3D.Set(15, -23, 0);
 
     number.GetComponent<RectTransform>().localScale = Vector3.one;
     number.GetComponent<RectTransform>().position.Set(10, 0.15f, 0);
     number.GetComponent<RectTransform>().localPosition.Set(10, 0.15f, 0);
 
     number.transform.position.Set(10, 0.15f, 0);
     number.transform.localPosition.Set(10, 0.15f, 0);
   }
 }

I've uploaded a few screenshots here that might help clarify my question.

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

2 People are following this question.

avatar image avatar image

Related Questions

Transform scale evenly from center to specific position 1 Answer

Change Text Scale on a Canvas Child from a script. 1 Answer

Distribute terrain in zones 3 Answers

Dialogue variables for different endings c# 0 Answers

How to increment value of axis when something happens 2 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