How to see if an objects current scale is default or enlarged

I am very new to C#… And Unity… I have just put little excerpts of this awfully coded code I am working on.

I am making a game where I am Increasing the size of an object. (the below is mostly me just checking on if the idea is working or not… It seems to be working but not like I need it to)

Vector3 resized = new Vector3(4f, 0.5f, 0.3f);
normalScale = GameObject.Find (“Player”).transform.localScale;

Vector3 testScale = GameObject.Find (“Player”).transform.localScale = (resized);

Debug.Log (normalScale);
Debug.Log (resized);

if (GameObject.Find (“Player”).transform.localScale == normalScale) {

large = false;

I need it to be

if (GameObject.Find (“Player”).transform.localScale == resized) {

large = false;

But I can’t figure out how to allow that to happen.

I’m not clear with what you are actually having issue with, it seems that you have the Vector3 resized variable in scope, so why is what you “need it to be” causing you problems?

I would however more broadly question what it is that you are trying to do. If, as the title suggest, you are more generally trying to determine if an object’s scale is greater than the default (which is Vector3(1, 1, 1)) then you’d be much better off comparing their magnitudes like this:

//Please use code tags to format code snippets
large = GameObject.Find("Player").transform.localScale.sqrMagnitude > 3; //Vector3.one.sqrMagnitude is always going to be 3

If that’s not what you’re looking for then you’ll need to explain in greater detail what it is that you’re trying to accomplish (I know you mentioned that you’re new to C# and Unity, so no worries).

Basically is my object is Vector3(4f,0.5f, 0.3f) then I need it to skip the enlarging of the object.

But for some reason I can’t figure out how to check if the object I’m looking at has that exact scale

If you’re checking for exact values then

if(GameObject.Find ("Player").transform.localScale == new Vector(4.0f, 0.5f, 0.3f))
{ ... }

//or, if you have a variable with your target values
Vector3 targetScale = new Vector(4.0f, 0.5, 0.3f);

if(GameObject.Find ("Player").transform.localScale == targetScale)
{ ... }

is doing exactly that. I’m still not seeing what the issue is. However - depending on how it is that you are enlarging the object, the comparison may very likely fail due to floating point precision errors. Now take a look at this script:

using UnityEngine;

public class ScaleTest : MonoBehaviour
{
    public Vector3 TargetScale = new Vector3(4.0f, 0.5f, 0.3f);
    public Vector3 StartingScale = Vector3.one;
    public float TimeToScale = 5.0f;

    private bool _continueScaling = true;
    private float _currentT = 0.0f;

    private void Start()
    {
        transform.localScale = StartingScale;
    }

    private void Update()
    {
        if(_continueScaling)
        {
            _currentT += Time.deltaTime / TimeToScale;
            if(_currentT >= 1.0f)
            {
                transform.localScale = TargetScale;
                _continueScaling = false;
            }
            else
            {
                transform.localScale = Vector3.Lerp(StartingScale, TargetScale, _currentT);
            }
        }
    }
}

If you attach it to an object and hit play, you’ll see it first scale to whatever your StartingScale is, then over a period of TimeToScale seconds it will change scale to TargetScale and finally stop. The important bit is that we’re using Vector3.Lerp to smoothly change from the starting scale to the target scale, and so we can know we’ve reached our target if our _currentT variable is ever greater than or equal to 1.0f (look at the link for Lerp to see why we can do that).

If you are changing your scale using some other method and you know you’re going to be comparing for exact values somewhere else in your code, then everytime you change the scale you need to check to see if you’ve gone too far and then need to set the values at your target or not - otherwise your exact value comparison will very likely fail.

I don’t know if that answers your question at all or is just more confusing. If I’ve missed it could you please post the entire script that you are using and pinpoint where you’re having problems?

public class PowerUpScript : MonoBehaviour {

	private GameObject Player;
	private Vector3 normalScale;

	public float timer = 5f;
	private bool large = false;

	public AudioClip powerup;

	void Start () {

		Vector3 resized = new Vector3(4f, 0.5f, 0.3f);
		normalScale = GameObject.Find ("Player").transform.localScale;

		Vector3 testScale = GameObject.Find ("Player").transform.localScale = (resized);

		Debug.Log (normalScale);
		Debug.Log (testScale);
	}
	
	void Update () {

		rigidbody.AddTorque( Vector3.forward * 10f );	

		if (GameObject.Find ("Player").transform.localScale == resized)
		{
			
			large = false;
			
		}
					}
	
		
	void OnCollisionEnter( Collision col ) 
		{

				if (this.gameObject.name == "bigger_powerup(Clone)") 
				{
				
						if (large == true) 
					{
								Destroy (gameObject);
								
					}
						if (large == false) 
						{
								timer = 5f;
								
								Vector3 temp = new Vector3 (4f, 0, 0);
								GameObject.Find ("Player").transform.localScale += (temp);
						}

				}

				Destroy (gameObject);
				AudioSource.PlayClipAtPoint(powerup,transform.position, .4f);
		}
}

Assets/scripts/Powerups/PowerUpScript.cs(29,72): error CS0103: The name `resized’ does not exist in the current context

It’s actually line 26 on here

that is the error I am getting

Sorry for the awful coding lol… but I have no idea why this isn’t allowing me to do what I am trying to do

OK - The error itself is due to the Update method trying to use a variable called resized that doesn’t exist. This is because you declare the variable inside the Start method and once a method is done executing all method declared variables go out of scope. You can move the variable declaration outside of the Start method and into the PowerUpScript class itself (similar to how you define the timer or large member variables in the class) and initialize it on Start so that it will remain in scope for when Update executes.

There are other issues with the code itself, but fixing the above will get you to where you actually compile. Now - what is it that you want to happen? Are you trying to resize the player when they pick up an object with this script attached?

I need it to resize the player on contact… Then run a 5 second timer. If it hits that object again it will restart the timer. When the timer runs out it will reduce the player size back to normal

Ok so I abandoned the idea of using the “large” bool and replaced them with the gameObject.Find… It is now correctly determining whether or now the object has been increased… Now I just need the timer to work… And it looks like I never even finished coding that part

Sounds like you’re figuring things out.

Let me suggest that you move your OnCollisionEnter method onto a script that you attach to the player object that checks for an object tagged “bigger_powerup” (instead of checking the actual name of the object). Define a Vector3 TargetScale member on the script and keep modifying localScale towards it in your Update method, which should also decrementing your timer by Time.deltaTime until it reaches zero. Then, when you collide with “bigger_powerup” you just set the TargetScale and timer, and if your timer ever reaches 0 you set your TargetScale to Vector3.one (or whatever your normal scale should be).

Also, be aware that methods like GameObject.Find are relatively costly - you should avoid using them in a regularly invoked method. Use them selectively and store their result in a member variable and use that variable from then on whenever you can.

Let me know if you need more specific help or explaining with something and good luck!

I appreciate the help! I actually had already switched it to the Player object because where I was destroying this object on collision the timer function wasn’t working properly. I need to set a variable still but that won’t be an issue at all.

Thanks again for the help.