I am creating a test project in Unity & I have an input field and a button. I would like to take the text from the input and simply Debug.Log to the Unity Console onclick of a button. All the examples show the variables and functions but don't show HOW they are getting the input to the variable. I already have UI set up including linking the function to the button. Is there some way I can give it an id like in JavaScript & HTML? I dont want to do anything fancy. Here is my code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class main : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
public void debugInput() {
Debug.Log()
}
}
I would like to pass the variable in the Debug.Log in the debugInput function.
Can someone help me?
Thanks in advance
The InputField uses a callback for entering text to an input field called OnSubmit. If you would rather print every frame the user enters new text, there is also OnValueChanged.
These callbacks can be assigned in the inspector by selecting your InputField object. The inspector should look something like the below photo.
Simply click the + button, drag in the object that has the script that has the method you would like to callback. Make sure when selecting it to choose it from the Dynamic String list as it will auto pass in the string you have in the InputField to your function. You will need to change your code to take a string parameter tho.
public void debugInput(string str)
{
Debug.Log(str)
}
If you would rather set these values through code, you can do so by adding new listener delegates to your object. I can post a snippet for this solution if you would rather keep everything in a script.
Edit: I am using a TMPro Input Field, but the above example still works for a Unity UI Input Field. Instead of OnSubmit, you can use OnEndEdit. You can also add your own EventTriggers to use OnSubmit if you want.
Related
Hi hope its not too dumb a question. I'm trying to see if i could drag the text in a button onto the on Click method of the button. The drag drop doesn't work.
If not, is the only way on script? Whats the best way to get the text via script?
(The text is added via a script).
Seemingly your DialogueManager.continue method takes a string as parameter
public void continue(string text)
{
...
}
-> No! You can't simply drag in a component into a string field.
Try to make your method rather take a Text/TMP_Text component as parameter
public void continue (Text /* or TMP_Text accordingly*/ text)
{
Debug.Log(text.text);
...
}
You can do something like that. The function you are calling needs to take what you want to pass as a parameter though.
So in this example the function is taking a GameObject.
public void DialogueButtonPressed(GameObject textGameObject)
{
}
You can pass whatever type you want though. Maybe you want to pass a UI.Text component.
I am creating an animation in my Unity Game using Playable Directors and Animators, and i need to animate the contents of a TextMeshPro so it changes its text within keyframe, but there is no Text property in the animator.
I initialy thought that this was just with TextMeshPro, and changed my component to a normal Text component. No property found either.
My last try was to create a new script called TextAnimator, and a string property that changes the TextMeshPro contents in editor using the [ExecuteInEditMode] annotation.
But when i tried to animate THIS property, it doesnt show up either!
Here is my component in game:
Here is when i try to add the string property:
Here is the TextAnimator:
using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
[ExecuteInEditMode]
public class TextAnimator : MonoBehaviour
{
public TextMeshProUGUI textUI;
[TextArea]
public string text;
void Update()
{
if (textUI) {
textUI.text = text;
}
}
}
It seems that Unity can't animate strings at all, why is that? And what can i do as a workaround to make my animations?
There is a simple solution without code. Consider using DOTween asset as alternative. Just add "DOTWeen Animation" component to your text object, set the animation type to "Text" and write the sentence that should gradually appear in "To" input field. See screenshot
I am building an AR app using AR Foundation but got stuck with changing the material of my model during runtime by pressing a button.
The script below works perfectly fine when I attach it to my model and then create buttons with OnClick() Events to access the specific material WHEN my model is in the hierarchy.
But I don't want it to stay in the hierarchy because otherwise it will constantly show up in AR. If I remove it from the hierarchy, the OnClick() is obviously missing a prefab.
So I need to have the possibility to still change materials from button clicks but without my model staying in the hierarchy. I think "AddListener" could be a solution, but I don't really know how to edit the script in order to make it work. With AddListener I wouldn't have to use OnClick().
Can anyone please show me the solution?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ChangeColor : MonoBehaviour
{
public Material[] material;
Material setMaterial;
Renderer rend;
void Start()
{
rend = GetComponent<Renderer>();
rend.enabled=true;
}
void Update()
{
}
public void GreyMaterial()
{
rend.sharedMaterial = material[0];
setMaterial = rend.material;
}
public void YellowMaterial()
{
rend.sharedMaterial = material[1];
setMaterial = rend.material;
}
public void RedMaterial()
{
rend.sharedMaterial = material[2];
setMaterial = rend.material;
}
}
You can out your prefab in folder called "Resources". And get your prefab using Resources.Load in script next way:
GameObject prefab = Resources.Load<GameObject>("Path"); // Path WITHOUT "Resources/"
Here prefab is... your prefab and you get change some components and values in it.
P.S.
It is very important to write Resources letter to letter without mistakes
You may put folders into (folders into folders...) into Resources, but here you must set the exact path to your file.
I believe the script is not referencing all the new instantiated objects, its needs to reference them. Either store those new objects in a list that the script can change, or try to search for every item of that material you want to change when you run the change material function(not very efficient, but might be easier)
i found this on youtube
I am making a customizable game using Unity 2019.3f. In my main menu, I have the option for people to input a number into an input field for the amount of ammo. The input field then converts that value to an int and sets a gameobject's transform position.x equal to it. For some reason, when I input a number into the input field and click apply settings, the input field completely disappears and I have to click Play and then go back to the main menu for the input field to reappear and then somehow start working and correctly assigning the right numbers to the right spots. I used Debug.Log to see if the value of the input field were convertible to an int but they were. My code to set the gameobject transform to the input field number is here:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
using TMPro;
public class GetInputValue : MonoBehaviour
{
public InputField player1ammoinput;
public InputField player2ammoinput;
public GameObject ammo1;
ammo1.transform.position = new Vector3(System.Convert.ToInt32(player1ammoinput.text), 0f, 0f);
ammo1.transform.localScale = new Vector3(System.Convert.ToInt32(player1ammoinput.text), 0f, 0f);
Debug.Log(player1ammoinput.text);
}
Can someone find out why unity is doing this? ammo1 is just the gameobject that stores the number. It is an empty Gamebject. The actual input field is a different gameobject which I refer to in this script as player1ammoinput.
If I understand this correctly you are parsing the text to an integer value, and then using that value to move the ammo1 object to the side (or towards the player).
I suggest moving the parsing to an extra variable before the actual adaptation, and think the value you get (say 22 ammo) will move the ammo1 item 22 units away, which is FAR, hence it disappears immediately from view.
I'm looking for a way to switch between input fields without having to double click the next input field twice.
What happens now is, for instance I type in something inside the first field, and then when I try to go to another field, I need to click on the other field once(or somewhere else besides the first input field) to remove focus, and then to click again on the other field to open up the keyboard and switch the focus to that field.
What can I do to make the switch between two input fields happen in one click(when clicking on the desired input field)
Thanks!
From your problem, it seems like you are working on Android/iOS devices.You could try calling TouchScreenKeyboard.Open() when the InputField is clicked, like so:
[RequireComponent(typeof(InputField))]
public class ShowKeyboardOnClick : MonoBehaviour {
private InputField inputField;
private void Awake() {
inputField = GetComponent<InputField>();
}
// Or raycast, etc
private void OnMouseDown() {
TouchScreenKeyboard.Open(inputField.text);
}
}
Note that from the documentation, it states:
Only native iPhone, Android, and Windows Store Apps are supported.