geting GameObject transform position gives Spasmic numbers

Hello everyone, here’s the code to my problem. I’ll put the description of the problem under it.

GameObject player;
public Vector2 playerPos;
public Vector2 shopPos;
public bool inRange = false;
public Vector2 distance;

// Use this for initialization
void Start () {
	player = GameObject.FindGameObjectWithTag("Player");
}

// Update is called once per frame
void Update () {
	
	
	playerPos = new Vector2(player.transform.position.x, player.transform.position.y);
	shopPos = new Vector2(transform.position.x, transform.position.y);
	
	distance = playerPos - shopPos;
	
	if(distance.x <= 3 && distance.x >= -3 &&
	   distance.y <= 3 && distance.y >= -3){
		inRange = true;
	}
	
	else{
		inRange = false;
	}
	OnGUI();
}

Okay so when i run this script the Y-Axis numbers are going crazy changing from 5 to -41 in a split second and then switching back to 5 and then down to -12, and keeps going like that.
Since the GameObject with the Tag “player” consists of many children(and the script works fine with just an ordernary Cube), I thought if I’d fill the GameObject “player” with the smallest child the spasms would stop but it didn’t.

I am completely Clueless.
Please healp me, Kamden.

I came across your question and I'm pretty bored, so I just made a game object with 6 children, 7 grandchildren, and 9 great-grandchildren lol ... but I didn't have any crazy number problems. I changed your script a bit, though, since I moved it over to UnityScript, so I don't know if there might be something weird in yours or what's going on there. I tried this with & without child objects/gravity/rigidbodies , no crazy numbers happening for me... Anyway, here's what I used in UniScript if you want to try it and see if it works how you want it to ->

var player : Transform ;
var shop : Transform ;
var playerPos : Vector2 ;
var shopPos : Vector2 ;
var range : float = 3.0 ;
var distance : float ;
var inRange : boolean = false ;
 
function Start(){
   player = GameObject.FindGameObjectWithTag("Player").transform ;
   shop = transform ;
}
 
function Update(){
   playerPos = Vector2(player.position.x , player.position.y) ;
   shopPos = Vector2(shop.position.x , shop.position.y) ;
   distance = Vector2.Distance(playerPos , shopPos) ;
 
   if(distance <= range){
      inRange = true ;
   }
   else{
      inRange = false ;
   }
Debug.Log(inRange) ;
}

Hmm… Like I said, I’m pretty bored, so I’m going to go ahead and try to put that into C# for you even though I don’t really know the language. I can translate it most of the time into UniScript when it’s not very complex like this one isn’t, but haven’t tried putting it back yet :smiley: Here goes →

Transform player;
Transform shop;
public Vector2 playerPos;
public Vector2 shopPos;
public float range = 3.0f; //<--- ????
public float distance;
public bool inRange = false;
 
void Start(){
   player = GameObject.FindGameObjectWithTag("Player").transform;
   shop = transform;
}
 
void Update(){
   playerPos = new Vector2(player.position.x, player.position.y);
   shopPos = new Vector2(shop.position.x, shop.position.y);
   distance = new Vector2.Distance(playerPos, shopPos);
   if(distance <= range){
      inRange = true;
   }
   else{
      inRange = false;
   }
}