Hello Guys, real noob here. I’ve been looking for hours online and in this forum but unfortunately seems like nobody had ever had this problem before, or maybe is way too stupid to post it.
I have to show some text fields when i click a button inside a very simple UI. The idea is to have a single textfield coming up every time i click the “next instructions” button like a sort of a sequence.
The Text fields have been divided one by one and put into an empty object just to contain them.
Obviously whenever is parent them with the panel object they all show up at once, which is what i’m trying to avoid.
First you should stick the Input Field inside 2 empty GameObjects. Call one Manager or something like that and have it at the very top of the Hierarchy and name the other one inputFieldHolder and have that where the input field is. Stick the input field into the inputFieldHolder then create a script called Show Input Field in the Manager GameObject you created earlier and copy and paste in this code:
using UnityEngine;
using System.Collections;
public class ShowInputField : Monobehavior {
public GameObject inputFieldHolder;
public void ShowInputField(){
inputFieldHolder.SetActive(true);
}
}
Once you save that you will notice that their is a field called inputFieldHolder in the script you just created. Drag the inputFieldHolder GameObject into that field and then you’re done with that part. The next parts easy: Go onto the button you want to use to open the input field. Scroll down to the bottom of that and you will see an OnClick() function. Click the + at the bottom of that and then a box should appear inside it. In the field where it says “runtime only” change that to “editor and runtime” and in the one below that drag the Manager GameObject into it.
Finally the bar on the right should be clickable. Go onto it and you should see a few options. The only one you want is the ShowInputField. When you go onto that some options will come up. Look for the ShowInputField() one and then you’re done. That should do it. If not don’t hesitate to reply by using the @the_game_maker
First off thanks a lot for the help, after few hours of silence i thought nobody was going to help me.
I think i’ve done what you’ve told me, but i’ve got an error in the script and is still not working. I’m not sure i’ve understood the first part of your message, so i’ve attached some screenshots in order for you to check whether or not i’ve done it properly.
I had to modify the script because monodevelop was throwing an error basically saying that the function name cannot be the same of the class, so here’s what i’ve done:
using UnityEngine;
using System.Collections;
public class ShowInputField : MonoBehaviour {
public GameObject inputFieldHolder;
public void NewShowInputField ()
{
inputFieldHolder.SetActive(true);
}
}
For the second part of your message i think i followed ok.
Probably i’m not positioning the text fields where they should be, the idea is to have them hidden in the beginning, and then as soon as i start pushing the button they should start appearing one by one, as you can see from the screenshot attached everything is there since the beginning.
I’ve tried to change inputFieldHolder.SetActive(true); into inputFieldHolder.SetActive(false); but all i get is that whenever i start and push the button everything disappear. What i would like to have is exactly the opposite, with the text field coming up one at the time every time i push the button.
this script might help you, it bases on your initial setup
using UnityEngine;
using System.Collections;
using System.Linq;
public class instructionScript : MonoBehaviour {
private GameObject[] instructionArray;
void Start ()
{
instructionArray = GameObject.FindGameObjectsWithTag("instructionText").OrderBy( go => go.name).ToArray();
for(int i=0; i < instructionArray.Length; i++)
{
instructionArray[i].SetActive(false);
}
}
public void revealNext()
{
for(int i=0; i< instructionArray.Length; i++)
{
if(!instructionArray[i].activeSelf)
{
instructionArray[i].SetActive(true);
return;
}
}
}
}
place this script in a empty game object (for example named “instructionManager”) and remember to set all your text fields with appropriate tag (for example mine is “instructionText”). In OnClick() inside the properties of the button, remember to place the “instructionManager” in the list and set to “use” revealNext();
If you have a fixed number of text fields, you might wanna try this with a public List instead. Remember to use the namespace System.Collections.Generic or else you can’t create a List.
If you don’t like Linq, you can write your own sorting function as “FindGameObjectsWithTag” returns an array with game objects in random order.
not sure what you mean with the above (if you could find 10 minutes to explain would be great ), but thank you, thank you, thank you very much, you solved the problem, is working just the way i wanted to.
you can see in the attachment, the you can allocate how many element are there in a list. So if you have a fixed number of things you wanna show in a certain sequence, you can set them directly in the editor and not to worry about the random-order-array which “FindGameObjectsWithTag” will provide
the scritp if you’re using List
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class instructionListScript : MonoBehaviour {
public List<GameObject> instructionList;
void Start()
{
foreach(GameObject item in instructionList)
{
item.SetActive(false);
}
}
public void revealNext()
{
for(int i=0; i<instructionList.Count; i++)
{
if(!instructionList[i].activeSelf)
{
instructionList[i].SetActive(true);
return;
}
}
}
}
I’m also bored enough to write 2 simple sorting methods, just sharing here
void exchangeSort(GameObject[] theArray)
{
int length = theArray.Length;
GameObject tempGO;
for(int i=0; i < length; i++)
{
for(int j=i+1; j<length; j++)
{
int tempInt1;
int tempint2;
int.TryParse(theArray[i].name, out tempInt1);
int.TryParse(theArray[j].name, out tempint2);
if(tempInt1 > tempint2)
{
tempGO = theArray[i];
theArray[i] = theArray[j];
theArray[j] = tempGO;
}
}
}
}
void bubbleSort(GameObject[] theArray)
{
int length = theArray.Length;
GameObject tempGO;
for(int j=length-1; j>0; j--)
{
for(int i=0; i<j; i++)
{
int tempInt1;
int tempint2;
int.TryParse(theArray[i].name, out tempInt1);
int.TryParse(theArray[i+1].name, out tempint2);
if(tempInt1 > tempint2)
{
tempGO = theArray[i];
theArray[i] = theArray[i+1];
theArray[i+1] = tempGO;
}
}
}
}
Performance wise, they are not the best, but should do if you don’t have a lot of things to sort. You might wanna google “sorting algorithm” if you’re interested.