- Home /
Looting Help
Major problem here. I am making a looting system. I may have to re-write it if this doesn't help. I am totally stumped on this one. I got the player to kill the enemy and instantiate the pouch, however, I don't know how to get it to destroy!!!!!!! I've tried GameObject.Find();, but that doesn't work because I have multiple objects. I also don't want to make individual prefabs, and also GameObject.Find(); cant find inactive objects. How dumb is that?!
Here is the collection piece:
#pragma strict
var minExp : float;
var maxExp : float;
var minGold : float;
var maxGold : float;
var hud : GameObject;
private var gold : int;
private var exp : int;
var expGui : GameObject;
var goldGui : GameObject;
function Start () {
exp = Random.Range(minExp, maxExp);
gold = Random.Range(minGold, maxGold);
hud = GameObject.Find("Loot Pouch");
expGui = GameObject.Find("exp");
goldGui = GameObject.Find("gold");
}
function OnTriggerStay (col : Collider) {
if(col.gameObject.tag == "Player" && Input.GetKeyDown(KeyCode.E)) {
hud.active = true;
expGui.guiText.text = "EXP: " + exp;
goldGui.guiText.text = "GOLD: " + gold;
}
}
Here is the take:
#pragma strict
var lootHud : GameObject;
function Start () {
lootHud = GameObject.Find("Loot Pouch Hud");
}
function OnMouseEnter () {
guiText.color = Color.red;
}
function OnMouseExit () {
guiText.color = Color.blue;
}
function OnMouseDown () {
lootHud.active = false;
}
You could try disabling the scripts on the object rather than setting the object to inactive. Then GameObject.Find may work. Or you could try using tags GameObject.FindGameObjectsWithTag
Answer by SterlingStudios · Aug 05, 2014 at 04:16 PM
Okay, so I got the loot HUD to show up by using guiText.enabled and guiTexture.enabled, but how do I delete a specific prefab? Because I want when you click 'take', the loot pouch destroys itself, however the pouch is instantiated so how can I tell which one to destroy?
if you use GameObject.FindGameObjectsWithTag it should give you an array of all of the instances. You could iterate through this array to find the closest object to the player, which should be the instance of the pouch you are taking from.
Hmmm... How would this work though? Would you $$anonymous$$d giving me an example?
From the documentation:
// Print the name of the closest enemy
print(FindClosestEnemy().name);
// Find the name of the closest enemy
function FindClosestEnemy () : GameObject {
// Find all game objects with tag Enemy
var gos : GameObject[];
gos = GameObject.FindGameObjectsWithTag("Enemy");
var closest : GameObject;
var distance = $$anonymous$$athf.Infinity;
var position = transform.position;
// Iterate through them and find the closest one
for (var go : GameObject in gos) {
var diff = (go.transform.position - position);
var curDistance = diff.sqr$$anonymous$$agnitude;
if (curDistance < distance) {
closest = go;
distance = curDistance;
}
}
return closest;
}
just add Destroy(closest.gameObject); Apply a tag to the pouch prefab, and change the tag on the code above (8th line).
Didn't work. Here is the current code:
#pragma strict
var exp : GameObject;
var lootBack : GameObject;
var gold : GameObject;
var destroy : boolean;
function Start () {
lootBack = GameObject.Find("LootPouchHolder");
exp = GameObject.Find("exp");
gold = GameObject.Find("gold");
}
function On$$anonymous$$ouseEnter () {
guiText.color = Color.red;
}
function On$$anonymous$$ouseExit () {
guiText.color = Color.blue;
}
function On$$anonymous$$ouseDown () {
lootBack.guiTexture.enabled = false;
exp.guiText.enabled = false;
gold.guiText.enabled = false;
guiText.enabled = false;
}
function FindClosestEnemy () : GameObject {
//Find all game objects with tag Enemy
var gos : GameObject[];
gos = GameObject.FindGameObjectsWithTag("Pouch");
var closest : GameObject;
var distance = $$anonymous$$athf.Infinity;
var position = transform.position;
// Iterate through them and find the closest one
for (var go : GameObject in gos) {
var diff = (go.transform.position - position);
var curDistance = diff.sqr$$anonymous$$agnitude;
if (curDistance < distance) {
closest = go;
Destroy(closest.gameObject);
distance = curDistance;
}
}
return closest;
}
add the following code at the end of On$$anonymous$$ouseDown:
FindClosestEnemy();
and add the following code replacing line 39:
var position = GameObject.Find("Player").transform.position;