Quantcast
Channel: Questions in topic: "declare"
Viewing all articles
Browse latest Browse all 78

Can't use declared variable to find nearest Object

$
0
0
Hello everybody, I want to "respawn" a player to the race track when he falls off the map and touches a trigger volume, this is my code : GameObject GetClosestObject(string tag) { GameObject ClosestObject; GameObject[] gos = GameObject.FindGameObjectsWithTag(tag); float distance = Mathf.Infinity; foreach (GameObject go in gos) { Vector3 diff = go.transform.position - transform.position; float curDistance = diff.sqrMagnitude; if (curDistance < distance) { ClosestObject = go; distance = curDistance; } } return ClosestObject; } But I get this error in the last line : > error CS0165: Use of unassigned local variable `ClosestObject' Anyone knows what I'm doing wrong?

Viewing all articles
Browse latest Browse all 78

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>