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

load sprite and resize after key

Discussion in 'Scripting' started by danicrem, Jan 22, 2016.

  1. danicrem

    danicrem

    Joined:
    Nov 9, 2014
    Posts:
    45
    Hello,
    I made this piece of code, attached to an empty object, position 0,0,0, rotation 0,0,0, scale 1,1,1. It has a Sprite render component and a scipt component with the script below. I have plugged the sprites I need to the public slots Plaatje1 an Plaatje 2. The script swaps the sprite according to the random number. Still it does not resize the sprite after I press the up-arrow. How do I make this script work?
    Code (CSharp):
    1. using    UnityEngine;
    2. using    UnityEngine.UI;
    3. using   System.Collections;
    4. public    class PushPull : MonoBehaviour {
    5.  
    6.     public Sprite plaatje1;//drop het juiste plaatje in de public slot van Plaatje 1
    7.     public Sprite plaatje2;
    8.  
    9.  
    10.     void Start ()
    11.          {StartSpel();}
    12.  
    13.     void StartSpel() {
    14.         int nummerPlaatje;
    15.  
    16.         nummerPlaatje = Random.Range(1, 3); //range moet 1 groter zijn dan maximaal aantal plaatjes
    17.         if (nummerPlaatje == 1) {Plaatje1();}
    18.         if (nummerPlaatje == 2) {Plaatje2();}
    19.                      }
    20.  
    21.  
    22.  
    23.     void Plaatje1(){
    24.  
    25.         this.gameObject.GetComponent<SpriteRenderer>().sprite = plaatje1;
    26.         if (Input.GetKey(KeyCode.UpArrow)) { this.gameObject.transform.localScale = new Vector3(5, 5, 5); }
    27.  
    28.  
    29.     }
    30.  
    31.     void Plaatje2(){
    32.  
    33.         this.gameObject.GetComponent<SpriteRenderer>().sprite = plaatje2;
    34.         if (Input.GetKey(KeyCode.UpArrow)) { this.gameObject.transform.localScale = new Vector3(5, 5, 5); }
    35.  
    36.     }
    37.  
    38.  
    39.         }
    40.  
     
  2. Matt-Roper

    Matt-Roper

    <Of The Graphics> Unity Technologies

    Joined:
    Oct 27, 2015
    Posts:
    106
    Hey Danicrem,

    There's nothing wrong with your code, should it meet the requirements. It's best to always use Input within Update unless you have a really specific circumstance.

    Currently your code is doing if > "you are holding up WHILE this method ("Plaatje") is calle > Scale object

    Remember, in this case both "Plaatje" are only called once.
     
  3. danicrem

    danicrem

    Joined:
    Nov 9, 2014
    Posts:
    45
    Hello Matt,
    Thank you for the reply. I have rewritten the code and now it works. Now I only need a way to reset the script after the player has scaled down the sprite. And a way to slower the time of scaling (so it scales in steps).
    Code (CSharp):
    1. using    UnityEngine;
    2. using    UnityEngine.UI;
    3. using   System.Collections;
    4. public    class PushPull : MonoBehaviour {
    5.  
    6.     public Sprite plaatje1;//drop het juiste plaatje in de public slot van Plaatje 1
    7.     public Sprite plaatje2;
    8.     public bool slecht;
    9.     float down = 0.2f;
    10.  
    11.  
    12.  
    13.     void Start ()
    14.          {StartSpel();}
    15.  
    16.     void StartSpel() {
    17.         int nummerPlaatje;
    18.  
    19.         nummerPlaatje = Random.Range(1, 3); //range moet 1 groter zijn dan maximaal aantal plaatjes
    20.         if (nummerPlaatje == 1) {Plaatje1();}
    21.         if (nummerPlaatje == 2) {Plaatje2();}
    22.                      }
    23.  
    24.     void Update () {
    25.         if (slecht == true && Input.GetKey(KeyCode.UpArrow)) {
    26.             this.gameObject.transform.localScale = new Vector3(5, 5, 5);
    27.         }
    28.         if (slecht == false && Input.GetKey(KeyCode.DownArrow)) {
    29.             this.gameObject.transform.localScale = new Vector3(down, down, down);
    30.         }
    31.     }
    32.    
    33.  
    34.     void Plaatje1(){
    35.  
    36.         this.gameObject.GetComponent<SpriteRenderer>().sprite = plaatje1;
    37.         slecht = true;
    38.  
    39.  
    40.     }
    41.  
    42.     void Plaatje2(){
    43.  
    44.         this.gameObject.GetComponent<SpriteRenderer>().sprite = plaatje2;
    45.         slecht = false;
    46.  
    47.     }
    48.  
    49.  
    50.         }
    51.  
     
  4. danicrem

    danicrem

    Joined:
    Nov 9, 2014
    Posts:
    45
    OK, I have a completely wordking script now that swaps sprites randomny, scales them and resets the scale. Perhaps it will be of use to thers too.
    Code (CSharp):
    1. using    UnityEngine;
    2. using    UnityEngine.UI;
    3. using   System.Collections;
    4. public    class PushPull : MonoBehaviour {
    5.  
    6.     public Sprite plaatje1;//drop het juiste plaatje in de public slot van Plaatje 1
    7.     public Sprite plaatje2;
    8.     public bool slecht;
    9.     float down = 0.2f;
    10.     float up = 5f;
    11.  
    12.  
    13.     void Start ()
    14.     {
    15.         StartSpel();
    16.     }
    17.  
    18.     void StartSpel() {
    19.         int nummerPlaatje;
    20.  
    21.         nummerPlaatje = Random.Range(1, 3); //range moet 1 groter zijn dan maximaal aantal plaatjes
    22.         if (nummerPlaatje == 1) {Plaatje1();}
    23.         if (nummerPlaatje == 2) {Plaatje2();}
    24.                      }
    25.  
    26.     void Update () {
    27.         if (slecht == true && Input.GetKey(KeyCode.UpArrow)) {
    28.             this.gameObject.transform.localScale = new Vector3(up, up, up);
    29.             StartCoroutine("Wait");
    30.  
    31.         }
    32.         if (slecht == false && Input.GetKey(KeyCode.DownArrow)) {
    33.             this.gameObject.transform.localScale = new Vector3(down, down, down);
    34.             StartCoroutine("Wait");
    35.         }
    36.     }
    37.  
    38.     IEnumerator Wait()
    39.     {
    40.      
    41.         yield return new WaitForSeconds(2);
    42.         this.gameObject.SetActive(false);
    43.         this.gameObject.transform.localScale = new Vector3(1, 1, 1);
    44.         this.gameObject.SetActive(true);
    45.         StartCoroutine("Start");
    46.  
    47.     }
    48.  
    49.     void Plaatje1(){
    50.         this.gameObject.GetComponent<SpriteRenderer>().sprite = plaatje1;
    51.         slecht = true;
    52.     }
    53.  
    54.  
    55.     void Plaatje2(){
    56.         this.gameObject.GetComponent<SpriteRenderer>().sprite = plaatje2;
    57.         slecht = false;
    58.     }
    59.  
    60.  
    61.         }
    62.  
     
    Matt-Roper likes this.