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

Change de inputfield value qith script

Discussion in 'Scripting' started by juan_589migue, Jun 22, 2021.

  1. juan_589migue

    juan_589migue

    Joined:
    Dec 31, 2020
    Posts:
    9
    Hi guys, thanks for read. I needing your help please

    i have two objects in project with input filed components, how can i acces to them from script and change its valu value?:
    here the code

    public void OnSave()
    {
    int w = Screen.width;
    int h = Screen.height;
    int.TryParse(input_width.text, out w);
    int.TryParse(input_height.text, out h);

    if(Screen.orientation == ScreenOrientation.Landscape)
    {


    }


    if(w < 100 || h < 100)
    {
    OnError("Width or height is not correct.");
    return;
    }
    if (tog_watermark.isOn)
    {
    snapShot.CaptureAndSaveToAlbum(w, h, cam, ImageType.JPG, tex_watermark, WatermarkAlignment.TOP_RIGHT);
    }
    else
    {
    snapShot.CaptureAndSaveToAlbum(w, h, cam, ImageType.JPG);
    }




    The idea is when the camera change to landscape orientation the value of W takes H value and H value takes the W value.

    Please help me!!!
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,768
  3. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,144
    Use code tags when posting code.
    If you want to swap values, assign the value of h to a temp variable, then assign the value of w to h, then the temp variable value to w.

    Pretty simple swap.
     
  4. juan_589migue

    juan_589migue

    Joined:
    Dec 31, 2020
    Posts:
    9
    thank you but that is not my question, please:
    i have two objects in project with input filed components, how can i acces to them from script and change its text value?
    Code (CSharp):
    1. public void OnSave()
    2. {
    3. int w = Screen.width;
    4. int h = Screen.height;
    5. int.TryParse(input_width.text, out w);
    6. int.TryParse(input_height.text, out h);
    7.  
    8. if(Screen.orientation == ScreenOrientation.Landscape)
    9. {
    10. //HELP HERE PLEASE
    11. }
     
  5. RadRedPanda

    RadRedPanda

    Joined:
    May 9, 2018
    Posts:
    1,593
    You need a reference to the objects you want to change, and then you'd do something like
    Code (CSharp):
    1. object1.GetComponent<TextField>().text = "something";
    Probably not entirely correct, but that should be the gist of it.
     
  6. juan_589migue

    juan_589migue

    Joined:
    Dec 31, 2020
    Posts:
    9
    thank you. i used

    Code (CSharp):
    1.  
    2. if(Screen.orientation == ScreenOrientation.Landscape)
    3.      {
    4.  
    5.      wobject.GetComponent<InputField>().text = h;
    6.      hobject.GetComponent<InputField>().text = w;
    7.      
    8.  
    9.         }
    but the console shows the errors :
    Assets\CaptureAndSave\Example\Camera\CaptureAndSaveCamera.cs(97,6): error CS0103: The name 'wobject' does not exist in the current context
    for both

    How can i call that objects in this script? thank you
     
  7. RadRedPanda

    RadRedPanda

    Joined:
    May 9, 2018
    Posts:
    1,593
    You first need a reference to the object, at the top of your code inside the class you can have
    public GameObject wobject;

    which will be your reference. Then you can go into the inspector, and click and drag the object into the new field.

    upload_2021-6-23_22-4-23.png upload_2021-6-23_22-3-28.png

    Like this. Then, when you reference wobject in your script, it will be the object you dragged into the field.
     
    juan_589migue likes this.
  8. juan_589migue

    juan_589migue

    Joined:
    Dec 31, 2020
    Posts:
    9
    Officially you are my CSharp Master. It works and teach me a lot. thank you so much