0

I have an error in the script. At first, when the program starts, everything is fine, all InputFields have coordinates Y -200. But when I delete objects, and then re-create already the Y coordinate -800.

Where is the error?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class SpawnText : MonoBehaviour
{
public int text_number = 20;
public GameObject TextPanel;
public GameObject toggle;
public GameObject ct;
private int k;
public GameObject[] obj;
public GameObject[] obj1;
public GameObject textTime;
public GameObject textPrice;
public GameObject InputFieldTime;
public GameObject InputFieldPrice;
private RectTransform rt;
private bool isSettings = false;

// Start is called before the first frame update
void Start()
{
    CreateProgram();
}
// Update is called once per frame

//Клик на кнопку настроек
public void SettingsOnClick()
{
    switch (isSettings)
    {
        case false:
            isSettings = true;
            textTime.SetActive(false);
            textPrice.SetActive(false);
            InputFieldTime.SetActive(false);
            InputFieldPrice.SetActive(false);
            for(int i = 0;i < text_number; i++)
            {
                Destroy(obj[i]);
                Destroy(obj1[i]);
            }
            break;
        case true:
            isSettings = false;
            textTime.SetActive(true);
            textPrice.SetActive(true);
            InputFieldTime.SetActive(true);
            InputFieldPrice.SetActive(true);
            CreateProgram();
            break;
    }


    }

private void CreateProgram()
{
    rt = ct.GetComponent<RectTransform>();
    int height;
    k = -60;
    height = text_number * 72;
    rt.sizeDelta = new Vector2(0, height);

    for (int i = 0; i < text_number; i++)
    {


        obj[i] = Instantiate(TextPanel, new Vector2(0, 0), Quaternion.identity) as GameObject;
        obj1[i] = Instantiate(toggle, new Vector2(0, 0), Quaternion.identity) as GameObject;

        obj[i].transform.SetParent(ct.transform);
        obj1[i].transform.SetParent(ct.transform);

        obj[i].transform.localPosition = new Vector2(-200, k);
        obj1[i].transform.localPosition = new Vector2(-420, k);

        k -= 70;
        obj[i].transform.localScale = new Vector2(1, 1);
        obj1[i].transform.localScale = new Vector2(1, 1);
        obj1[i].GetComponentInChildren<Text>().text = (i + 1).ToString();
    }
}
}

When program starts

When delete and create objects

0

1 Answer 1

1

It is not Y but X actually

But note that the RectTransform is not the same as Transform.

What you see there in the Inspector is afaik the RectTransform.anchoredPosition not the transform.localPosition!

Not 100% sure but you should try and use

obj[i].GetComponent<RectTransform>().anchoredPosition = new Vector2(-200, k);
obj1[i].GetComponent<RectTransform>().anchoredPosition = new Vector2(-420, k);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.