Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

How do I change game Object scaling using an inputfield?

Discussion in 'Scripting' started by PedramCarter, Dec 6, 2016.

  1. PedramCarter

    PedramCarter

    Joined:
    Nov 1, 2015
    Posts:
    15
    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.
     
    Last edited: Dec 6, 2016
  2. neginfinity

    neginfinity

    Joined:
    Jan 27, 2013
    Posts:
    13,301
  3. PedramCarter

    PedramCarter

    Joined:
    Nov 1, 2015
    Posts:
    15
    Don't I need to assign the y and x and z value e.g.
    Code (csharp):
    1.  
    2. 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.
     
  4. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,802
    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.
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3. public class Example : MonoBehaviour {
    4.  
    5.     // assign in the inspector
    6.     public InputField inputFieldX;
    7.     public InputField inputFieldY;
    8.     public InputField inputFieldZ;
    9.  
    10.     private void OnEnable() {
    11.         // whenever these inputfields are edited, call these functions
    12.         inputFieldX.onEndEdit.AddListener(OnXEdited);
    13.         inputFieldY.onEndEdit.AddListener(OnYEdited);
    14.         inputFieldZ.onEndEdit.AddListener(OnZEdited);
    15.     }
    16.  
    17.     private void OnDisable() {
    18.         inputFieldX.onEndEdit.RemoveListener(OnXEdited);
    19.         inputFieldY.onEndEdit.RemoveListener(OnYEdited);
    20.         inputFieldZ.onEndEdit.RemoveListener(OnZEdited);
    21.     }
    22.  
    23.     private void OnXEdited(string newText) {
    24.         Vector3 newScale = transform.localScale;
    25.         newScale.x = float.Parse(newText);
    26.         transform.localScale = newScale;
    27.     }
    28.  
    29.     private void OnYEdited(string newText) {
    30.         Vector3 newScale = transform.localScale;
    31.         newScale.y = float.Parse(newText);
    32.         transform.localScale = newScale;
    33.     }
    34.  
    35.     private void OnZEdited(string newText) {
    36.         Vector3 newScale = transform.localScale;
    37.         newScale.z = float.Parse(newText);
    38.         transform.localScale = newScale;
    39.     }
    40. }
    I think you would do well to watch some Unity UI tutorials, as well as some fundamental C# tutorials.
     
    LeftyRighty likes this.
  5. PedramCarter

    PedramCarter

    Joined:
    Nov 1, 2015
    Posts:
    15
    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.
     
  6. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    ...
     
  7. PedramCarter

    PedramCarter

    Joined:
    Nov 1, 2015
    Posts:
    15
    I was talking about the inputfield because he said it's more appropriate for text, no need to be sarcastic.
     
  8. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,802
    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.
    https://unity3d.com/learn/tutorials/topics/user-interface-ui/ui-slider