Hi.I’m trying to make a text-related project and i have this page class with components ui panel and a text indicating its page number.In Add class when i press p panel appears with page number on it and it takes position at the right of last page.
Now my problem is whenever i start the game and press p the first page appears with a random number on it.And afterwards other pages comes out correctly as 1,2,3 etc.I’m guessing it has something to do with last play but i couldn’t find out what.
As my second problem i can modify panels position in update when i press p.But then if i try to do something with order() all elements of pages_list seems to be null so it doesn’t work.And i can’t modify anything about those panels.However pages_list.Count gives correct answer.
Thank you in advance.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Page : MonoBehaviour {
public int page_number{get; set;}
public GameObject panel_as_page { get; set; }
public Text page_num_text { get; set; }
public Canvas main_canvas;
public Page(int page_number, GameObject panel_as_page,Canvas main_canvas, Text page_num_text)
{
this.page_number = page_number;
this.panel_as_page = panel_as_page;
this.page_num_text= page_num_text;
this.main_canvas = main_canvas;
GameObject panel=(GameObject) Instantiate(panel_as_page);
panel.transform.SetParent(main_canvas.transform,false);
page_num_text.text = "Page "+page_number.ToString();
}
public void MakePageNumber(Page page, List<Page> pages_list)
{
page.page_number = pages_list.Count + 1;
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Add : MonoBehaviour {
private List<Page> pages_list;
public Canvas canvas;
public GameObject panel;
public Text page_num;
private Rect rt;
private float x, y;
void Start () {
pages_list = new List<Page>();
}
// Update is called once per frame
void Update () {
if (Input.GetKeyDown(KeyCode.P))
{
Page page = new Page(pages_list.Count + 1, panel,canvas,page_num);
pages_list.Add(page);
Debug.Log(page);
page.panel_as_page.name = page.page_number.ToString();
panels[1 ] = page.panel_as_page.gameObject;
x = 1000*( pages_list.Count -1);
y = 0f;
page.panel_as_page.GetComponent<RectTransform>().anchoredPosition = new Vector2(x,y);
}
if (Input.GetKeyDown(KeyCode.A))
{
order();
}
}
private void order()
{
foreach (Page page in pages_list)
{
if (panel)
{
page.panel_as_page.GetComponent<RectTransform>().anchoredPosition = new Vector2(100, 0);
Debug.Log(panel.name);
}
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class AddPage : MonoBehaviour
{
private List<Page> pages_list;
public Canvas canvas;
public GameObject panel;
// public Text page_num;
private Rect rt;
private float x, y;
void Start()
{
pages_list = new List<Page>();
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.P))
{
GameObject newpage = (GameObject)Instantiate(panel);
Page page = newpage.AddComponent<Page>();
page.Init(pages_list.Count + 1, canvas);
pages_list.Add(page);
Debug.Log(page);
page.panel_as_page.name = page.page_number.ToString();
// panels[1] = page.panel_as_page.gameObject;
x = 1000 * (pages_list.Count - 1);
y = 0f;
page.panel_as_page.GetComponent<RectTransform>().anchoredPosition = new Vector2(x, y);
}
if (Input.GetKeyDown(KeyCode.A))
{
order();
}
}
private void order()
{
foreach(Page page in pages_list)
{
page.panel_as_page.GetComponent<RectTransform>().anchoredPosition = new Vector2(100, 0);
Debug.Log(page.panel_as_page.name);
// Because I'm not sure what the panel check was for in the loop below.. I was curious if this
// did what you wanted.
}
}
}
So, I used a gameobject (prefab) which consists of: A panel and a child Text object. Hopefully that’s right.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Page : MonoBehaviour
{
public int page_number { get; set; }
public GameObject panel_as_page { get; set; }
public Text page_num_text { get; set; }
public Canvas main_canvas;
public void Init(int page_number, Canvas main_canvas)
{
this.page_number = page_number;
this.panel_as_page = gameObject;
this.page_num_text = GetComponentInChildren<Text>();
this.main_canvas = main_canvas;
// GameObject panel = (GameObject)Instantiate(panel_as_page);
transform.SetParent(main_canvas.transform, false);
page_num_text.text = "Page " + page_number.ToString();
}
public void MakePageNumber(Page page, List<Page> pages_list)
{
page.page_number = pages_list.Count + 1;
}
}
apologies for not trimming my response. I will try to summarize. I got rid of the public constructor, because that’s usually not great, and swap it for ‘Addcomponent’ with an Init method.
Changed the ‘panel’ check and the variable in the log inside the ‘Order()’ method, because that variable was local and I think you wanted the one from the list, instead. Seems to be working for me