Reputation: 5
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
public class spawningunits : MonoBehaviour
{
public static int amount1 = 100;
public static int amount2 = 200;
public GameObject ShadowWolf;
public GameObject Cannibal;
//public GameObject numberofunits1;
//public GameObject numberofunits2;
[SerializeField] private GameObject numberofunits1;
[SerializeField] private TMP_Text numberofunits2;
GameObject someObj;
private void Start(){
//numberofunits1.text = "200";
numberofunits2.text = "600";
Instantiate(numberofunits1, new Vector3(50, -1, -5), Quaternion.identity);
Instantiate(numberofunits2, new Vector3(45, -1, -5), Quaternion.identity);
Instantiate(ShadowWolf, new Vector3(56, -1, -5), Quaternion.identity);
ShadowWolf.SetActive(true);
Instantiate(Cannibal, new Vector3(-56, -1, -5), Quaternion.identity);
Cannibal.SetActive(true);
someObj = new GameObject("SomeObj");
numberofunits1.transform.parent = someObj.transform;
//GameObject someObj = new GameObject("SomeObj");
//someObj.AddComponent(numberofunits1);
//Vector3 pozycjacannibala = Cannibal.transform.position;
//Vector3 pozycjashadowwolfa = ShadowWolf.transform.position;
//numberofunits1.text = "200";
numberofunits2.text = "100";
}
void Update() {
//GameObject someObj = new GameObject("SomeObj");
//numberofunits1.transform.parent = someObj;
//Gameobject numberofunits1g = Gameobject.Find("numberofunits1g").GetComponent<GameObject>();
//Vector3 pozycjacannibala = Cannibal.transform.position;
//Vector3 pozycjashadowwolfa = ShadowWolf.transform.position;
//numberofunits1.transform.position = new Vector3(20,-1,-5);
numberofunits2.transform.position = new Vector3(Cannibal.transform.position.x,Cannibal.transform.position.y,Cannibal.transform.position.z);
numberofunits2.GetComponent<RectTransform>().localPosition += new Vector3(20,-1,-5);
//numberofunits1.GetComponent<RectTransform>().localPosition = new Vector3(-56,-1,-5);
//Vector3 poz = numberofunits1.transform.position;
//print(poz);
someObj.transform.position = new Vector3(Cannibal.transform.position.x,Cannibal.transform.position.y,Cannibal.transform.position.z);
//numberofunits1.transform.position = new Vector3(40, 12, -5);
//numberofunits1.anchoredPosition = new Vector2(20,-1);
//Vector3 poz = numberofunits1.transform.position;
//print(poz);
}
}
What I want to do is move text to a certain position (the text is a prefab, I'm using text mesh pro). The problem I get is that instantiation works correctly setting up the text in a desired position, however I cant seem to be able to move it later on. The code doesn't throw any errors and I even set a print function to see what position does the code display and the position is correct, but on the screen the text is still in the same place. The project is being created in 2d.
Upvotes: 0
Views: 2913
Reputation: 716
TextMeshPro doesn't contain transform component but rect transform so you need to chanage rect trasnform Or you can either create a gameObject like
[SerializeField] private GameObject numberofunits1;
[SerializeField] private GameObject numberofunits2;
GameObject someObj;
void Start(){
someObj = new GameObject("SomeObj");
numberofunits1.transform.parent = someObj;
}
void Update()
{
numberofunits1.text = "200";
numberofunits2.text = "100";
//Gameobject numberofunits1g = Gameobject.Find("numberofunits1g").GetComponent<GameObject>();
//Vector3 pozycjacannibala = Cannibal.transform.position;
//Vector3 pozycjashadowwolfa = ShadowWolf.transform.position;
numberofunits2.GetComponent<RectTransform>().localPosition += new Vector3(20,-1,-5) * Time.deltTime;
someObj.transform.position += new Vector3(Cannibal.transform.position.x,Cannibal.transform.position.y,Cannibal.transform.position.z) * Time.deltaTime;
Vector3 poz = numberofunits1.GetComponent<RectTransform>().localPosition.transform.position;
print(poz);
}
Upvotes: 0
Reputation: 21
When moving UI elements you will need to use the RectTransform instead of the regular transform (however the regular transform will still work for world space ui).
For example you could use: numberofunits1.GetComponent().anchoredPosition = new Vector2(20,-1);
if you really need the z position you can use: numberofunits1.GetComponent().anchoredPosition3D = new Vector3(20,-1,-5);
You can find the documentation on RectTransform here: https://docs.unity3d.com/ScriptReference/RectTransform.html
I also recommend using the TextMeshPro variable for the UI element you are trying to use. So in this case: TMP_Text.
So your variable declaration would become: [SerializeField] private TMP_Text numberofunits1; [SerializeField] private TMP_Text numberofunits2;
If you're trying to move the full set of UI it might be useful if you move the regular transform on the Canvas. However in that case the canvas needs to be World Space.
Upvotes: 2