r/UnityHelp • • Mar 03 '22

PROGRAMMING Issue with code. Basically I want there to be a value that represents the chosen objects localScale x and y value.

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

public class circleScale : MonoBehaviour

{

[SerializeField]

private Transform transformScale;

private void Start()

{

private float ratioX = transformScale.localScale.x;

}

}

This says that ratioX is reffering to a non static variable or something.

Also it says that it is missing an end bracket...

3 Upvotes

1 comment sorted by

1

u/Bonejob Code Guru Mar 03 '22

Scale is a vector3, to get a scale for a specific object you would need to get a reference to the transform component for the object you want the scale from and then grab the localscale value.

// gets the local scale of an object if you are not sure if the object has a transform
// all object do but meh clean code and all that
vector3 objectTransform = gameObject.GetComponent(typeof(Transform)) as Vector3;
vector3 local = objectTransform.localScale;

you can reference directly if you choose

vector3 objectScale = OTHER_GAMEOBJECT.gameObject.transform.localScale;