Reputation: 49
I am developing a game where players take turns in spelling a word. I have an input field prefab that is added anytime the player presses a button. Here is what I have done so far: These lines of codes instantiate a new input field when the user clicks the done button
//show next input field to enter letter
private void ShowInputField(){
GameObject inputFieldGameObject = inputFieldObjectPool.GetObject();
inputFieldGameObject.transform.SetParent (inputFieldParent);
Canvas.ForceUpdateCanvases ();
GameObject.Find ("ScrollView").GetComponent<ScrollRect> ().horizontalNormalizedPosition = 1;
}
Here is code for the done button which calls the ShowInputField function and a NextPlayerTurn function:
public void DoneButton(){
NextPlayerTurn ();
ShowInputField ();
}
Now what I want to add is when the done button is pressed the previous instantiated clones of the prefab should be set as inactive with the newly added instantiated prefab the only active prefab so that players cannot edit previously entered letters.
Any help would be kindly appreciated. Thank you in advance.
Upvotes: 0
Views: 220
Reputation: 125275
You have to create a temporary GameObject to hold the last InputField
. Check if it is null
after the Button is pressed.
If it is null, there is no last input. If it is not null, there is a last input, get the InputField
attached to it with lastInput.GetComponent<InputField>();
then use InputField.interactable
or InputField.readOnly
to disable the last InputField
so that players cannot modify it.
Other variable options you have is the InputField.enabled
variable. You have to test and decide which one of these variables work best for you.
GameObject lastInput = null;
void DeActivateLastInput()
{
if (lastInput != null)
{
InputField inputField = lastInput.GetComponent<InputField>();
inputField.interactable = false;
inputField.readOnly = true;
}
}
//show next input field to enter letter
private void ShowInputField()
{
GameObject inputFieldGameObject = inputFieldObjectPool.GetObject();
//Reset this pool since it may have been disabled before from the DeActivateLastInput function
InputField inputField = inputFieldGameObject.GetComponent<InputField>();
inputField.interactable = true;
inputField.readOnly = false;
//Make this Input Last Input
lastInput = inputFieldGameObject;
inputFieldGameObject.transform.SetParent(inputFieldParent);
Canvas.ForceUpdateCanvases();
GameObject.Find("ScrollView").GetComponent<ScrollRect>().horizontalNormalizedPosition = 1;
}
public void DoneButton()
{
NextPlayerTurn();
DeActivateLastInput();
ShowInputField();
}
Upvotes: 1