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 to check transform.localscale in if statement.

Discussion in 'Scripting' started by Slaghton, Jul 22, 2014.

  1. Slaghton

    Slaghton

    Joined:
    Sep 9, 2013
    Posts:
    139
    I'm just setting up a simple script to increase vegetation over time and I'm wondering what the correct syntax to use in the if statement.

    Everything works without the if statement. I just want to add it in so if its scale is <= 2 the script shuts off and the plant stops growing. Should be the only line I need help with.

    Code (csharp):
    1.  
    2. var plant : Transform;
    3. var speed : float = 1;
    4.  
    5. function Update () {
    6. //
    7. transform.localScale += Vector3.one * speed * Time.deltaTime * .01;
    8.  
    9. if(transform.localScale <= 2){   //This is what I need to correct
    10.         Debug.Log("Done");
    11. }
    12. }
    13.  
    Temporary Fix
    Well, I'd rather be able to tell when the transform was at a certain size to cancel the script but for now I just stuck a timer on it to shut down the script. Downside is I have to calculate how much time will pass before it gets to its full grown size which wastes a bit of time.

    Code (csharp):
    1.  
    2. var plant : Transform;
    3. public var counter : float = 0;
    4. var speed : float = 1;
    5.  
    6.  
    7. function Start () {
    8.   doneGrowing = false;
    9. }
    10.  
    11.  
    12.  
    13. function Update () {
    14.  
    15. transform.localScale += Vector3.one * speed * Time.deltaTime * .01;
    16.     counter++;
    17.    
    18.     if(counter > 1000){
    19.         doneGrowing = true;
    20.         counter = 0;
    21.         StartCoroutine("Kill_Script");
    22.         }
    23. }
    24.  
    25.     function Kill_Script(){
    26.     GetComponent("Plant_Growth").enabled = false;
    27.     }
    28.  
     
    Last edited: Jul 23, 2014
  2. zombiegorilla

    zombiegorilla

    Moderator

    Joined:
    May 8, 2012
    Posts:
    8,952
    You are trying to compare it to a int, you should be comparing to a vector3. if all the scales are the same, you could transform.localScale.x for example.
     
    Tonymotion and Slaghton like this.
  3. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,738
    You probably want to just use any one of the three numbers in the scale to compare. e.g. transform.localScale.x

    If you want to take into account all three numbers, you can use transform.localScale.magnitude. Note that this won't give you a value of 1 for (1,1,1), but (if I recall my formulas correctly) the cube root of 3, as it's basically going to give you the distance from (0,0,0) to (1,1,1).

    If you are doing this a lot (e.g. hundreds of times per frame), you will want to optimize it; .magnitude is a pretty slow operation because it has to calculate a square root. You can use .sqrMagnitude, which returns the magnitude squared. This is useful because this
    Code (csharp):
    1. if (transform.localScale.sqrMagnitude < 2*2)
    is MUCH faster than this:
    Code (csharp):
    1. if (transform.localScale.magnitude < 2)
     
    Tonymotion and Slaghton like this.
  4. Slaghton

    Slaghton

    Joined:
    Sep 9, 2013
    Posts:
    139
    Gah, I knew it was something simple. Worked like a charm zombie. (still learning the simple stuff lol)

    Ah, I didn't know about this. I'm going to take this into consideration and adopt my code appropriately!
     
  5. the1marauder

    the1marauder

    Joined:
    Jan 6, 2018
    Posts:
    1
    @zombiegorilla Thanks! You just answered a question I've been beating my head against a wall for 30 minutes on.