Okay so I can’t wrap my head around this issue. I have an empty game object in the scene called WeaponSpawner. This gameobject has 10 children called spawn_point and number at the end (spawn_point1, spawn_point2… etc). Every spawn point has a script attached to it to check distance between the spawn point and the player. Basically, what I’m trying to achieve is to display GUITexture to let the player know that they are near object (distance < 5) and they can pick it up. But the problem is that there are 10 objects with 10 scripts in the scene so it’s not reliable. I tried to use OnCollisionEnter and onTriggerEnter but they work in a really… ehm… funny way to say at least. How would you solve this problem?
A simple solution is to add a sphere collider to each spawn_point object, set its Radius to 5 and set its Is Trigger property. Create the GUITexture object - but don’t child it to anything! Finally, attach to the player a script like this one:
public var gTexture: GUITexture; // drag here the GUITexture object from the Hierarchy view
function Start(){
gTexture.enabled = false; // hide the texture at start
}
function OnTriggerEnter(other: Collider){
if (other.CompareTag("SpawnPoint")){ // if entering a SpawnPoint object...
gTexture.enabled = true; // show the GUITexture
}
}
function OnTriggerExit(other: Collider){
if (other.CompareTag("SpawnPoint")){ // if leaving a SpawnPoint object...
gTexture.enabled = false; // hide the GUITexture
}
}
For this to work, you must tag the spawn_point objects as “SpawnPoint”. Additionally, the player must be CharacterController or a Rigidbody.