How do I change game Object scaling using an inputfield?

How would the code go? and how would I enable it to pickup the answer? using enter? or a button to confirm the input within the number field?, also would I need 3 inputfields for x y z value?
Edit:
I forgot to mention I want this scaling inputed before the object spawns as I’ve created a map editor which results in creating multiple prefab clone(s), so just using the name or input tag will not work, I have buttons that simply change prefab selection to spawn different prefabs before spawning them.
Edit:
I know how to do it now but I don’t know what the code would go like?
I need it to change x y and z value need help with the code.
An apology in advance if it’s in the wrong section.

The “code would go” as

yourObject.transform.localScale = whatever_you_want;

Don’t I need to assign the y and x and z value e.g.

prefab.transform.localScale = new Vector3 (transform.localScale.y);

?
I’m currently using a public gameObject that has assigned prefab dragged onto the variable within the inspector.
Also “whatever_you_want;” is that a variable? how do I actually write that ? do I declare a variable on top of my field called? or do I just assign e.g. *(yourObject.transform.localScale = (2, 2 , 2);" ?
Don’t they need to be blank numbers? because I’m entering them in the GUI inputfield within run time, how do I declare the end of your called = “whatever you want” part.

Firstly, an InputField is not the ideal UI element for numerical input. It’s a string of characters. You would need to have a reference to the InputField, detect when the text has been changed, then parse the text for numbers and apply that as the scale.

Here’s an example of how you could set up parsing the text for numbers. This assumes you have 3 input fields, one for X, Y, and Z.

using UnityEngine;
using UnityEngine.UI;
public class Example : MonoBehaviour {

    // assign in the inspector
    public InputField inputFieldX;
    public InputField inputFieldY;
    public InputField inputFieldZ;

    private void OnEnable() {
        // whenever these inputfields are edited, call these functions
        inputFieldX.onEndEdit.AddListener(OnXEdited);
        inputFieldY.onEndEdit.AddListener(OnYEdited);
        inputFieldZ.onEndEdit.AddListener(OnZEdited);
    }

    private void OnDisable() {
        inputFieldX.onEndEdit.RemoveListener(OnXEdited);
        inputFieldY.onEndEdit.RemoveListener(OnYEdited);
        inputFieldZ.onEndEdit.RemoveListener(OnZEdited);
    }

    private void OnXEdited(string newText) {
        Vector3 newScale = transform.localScale;
        newScale.x = float.Parse(newText);
        transform.localScale = newScale;
    }

    private void OnYEdited(string newText) {
        Vector3 newScale = transform.localScale;
        newScale.y = float.Parse(newText);
        transform.localScale = newScale;
    }

    private void OnZEdited(string newText) {
        Vector3 newScale = transform.localScale;
        newScale.z = float.Parse(newText);
        transform.localScale = newScale;
    }
}

I think you would do well to watch some Unity UI tutorials, as well as some fundamental C# tutorials.

1 Like

Thank you for your kind response and taking your time to read this, I have watched few youtube tutorials on UI, which is how I got my score text and HP bar to work, but nothing relevant regarding this. If inputfield isn’t appropriate or best UI to use, then can yo kindly explain what element of UI would be? also how would the code go? similar ? would it be much more shorter and similar? Thanks again.

…

I was talking about the inputfield because he said it’s more appropriate for text, no need to be sarcastic.

You could use a Slider in the same way (listening for an event, probably onValueChanged for Slider), without the need to parse because a Slider will return a float instead of a string. The slider is usually complimented by a Text field showing the current value.

This is one of many tutorials that Unity provides, and there’s plenty more on the interwebs.