- Home /
(Solved) SetDestination() only works for Enemy tagged GO, but for the rest, it doesnt (possible unity bug?)?
Hello, i have a script were theres a SerDestination on it. Though the SetDestination only works for gameobjects tagged as Enemy, but for the rest, it doesnt work. When a gameobject is tagged other thing then Enemy, the SetDestination doesnt work (though i already changed the target of the SetDestination to the correct tag), but if i change the exact game object tag to enemy (i also change SetDestination target to objects with tag enemy), it works perfectly. Any help? Is this an possible unity bug?
agent.SetDestination(GameObject.FindGameObjectWithTag("Core").transform.position); // set new destination, to the core
This code actually works when its:
agent.SetDestination(GameObject.FindGameObjectWithTag("Enemy").transform.position); // set new destination, to the core
Yes, i changed the name of the gameobject and stuff to "Enemy"/"Core", but still, only "Enemy" works.
Heres the link for a video i made about this glitch
Entire Script:
using UnityEngine;
using System.Collections;
public class SoldierScript : MonoBehaviour {
public float Speed; //Character speed
public int RotationSpeed; //Character Rotation speed
public PauseScript pause; //Pause script
public string BRName; //Name of the character in portuguese
public int Health; //Health
public int MaxHealth; //Max health
public Rigidbody projectile; //Projectile that he will be shooting
public Transform Shooter; //Were this projectile will be shooting
public float stopOnDistance; //Distance of character and enemy that the character will stop
public float ShootDistance; //Distance that hes allowed to shoot
public float shootForce; //Force of the projectile
public float reloadTime; //Delay of the projectile shot
public Transform target; //Enemy
public GameObject particle; //Particle
public TextMesh text; //Health text mesh
public GameObject Core;
NavMeshAgent agent; //NavMeshAgent
public GameObject[] taggedGameObjects; //All of the enemys
private bool reloadNeeded = false; //Does it needs the delay
private bool stop; //Stop for the shot delay (this section is in Update, its necessery so it doesnt over do the Reload)
private bool stop2 = false; //Other stop for the healing process(also in update section)
private bool healing = false; //Does it need healing (when hes below 30% of health)
//Create a delay for the shot
IEnumerator Reload(){
yield return new WaitForSeconds (reloadTime);
reloadNeeded = false;
stop = false;
}
//Set a new target
void GetNearestTaggedObject() {
taggedGameObjects = GameObject.FindGameObjectsWithTag("Enemy");
float nearestDistanceSqr = Mathf.Infinity;
GameObject[] otherSoldiers = GameObject.FindGameObjectsWithTag("Soldier");
Transform nearestObj = null;
for(int c = 0;c<taggedGameObjects.Length;c++)
{
Vector3 ObjPos = taggedGameObjects[c].transform.position;
float distancesqr = (ObjPos - transform.position).sqrMagnitude;
bool SoldierTargetTrue = false;
for(int v = 0;v<otherSoldiers.Length;v++){
if(otherSoldiers[v].GetComponent<SoldierScript>().target == taggedGameObjects[c].transform&&otherSoldiers.Length<=taggedGameObjects.Length&&otherSoldiers[v] != gameObject)
SoldierTargetTrue = true;
}
if (distancesqr < nearestDistanceSqr&&!taggedGameObjects[c].GetComponent<EnemyScript>().OnBridge&&!SoldierTargetTrue) {
nearestObj = taggedGameObjects[c].transform;
nearestDistanceSqr = distancesqr;
}
}
target = nearestObj;
}
void Awake(){
pause = GameObject.Find ("Character").GetComponent<PauseScript>();
agent = gameObject.GetComponent<NavMeshAgent>();
Health = MaxHealth;
Core = GameObject.Find("Core");
}
//Heal when the soldier is close to the core
IEnumerator Heal(){
while(Health<MaxHealth&&(transform.position - GameObject.FindGameObjectWithTag("Enemy").transform.position).sqrMagnitude < 20){
Health ++;
yield return new WaitForSeconds(0.3f);
}
stop2 = false;
}
//Destination setter
void SetDest(Transform destination){
agent.SetDestination(destination.position); //Set new destination
print(agent.destination); // print the agents current destination
}
//Damage receiver
public void Damage(int damage){
Health -= damage;
GameObject part = (GameObject)Instantiate(particle,transform.position,transform.rotation);
Destroy(part,1f);
}
void Update () {
//When its close to the core
if((transform.position - GameObject.FindGameObjectWithTag("Core").transform.position).sqrMagnitude < 20&&Health<MaxHealth&&stop2 == false){
StartCoroutine(Heal());
stop2 = true;
}
healing = true; // So it doesnt move to the target, and yes to the core, to prevent target-core conflicts
agent.SetDestination(GameObject.FindGameObjectWithTag("Core").transform.position); // set new destination, to the core
text.text = Health + "/" + MaxHealth;
text.gameObject.transform.rotation = Quaternion.LookRotation(Vector3.forward);
transform.position = new Vector3 (transform.position.x,1.0059f,transform.position.z);
GetNearestTaggedObject();
//stuff you dont need to know about
if(Health <= 0){
Destroy(gameObject);
}
if(target == true&&!healing){// if target is true and its not in state of almost death
if((transform.position - target.position).sqrMagnitude > stopOnDistance&&Health>0&&agent.destination != target.position){// if for other stuff
//Set destination number 1 (Working), for enemies
//SetDest(target);
}
//And other stuff ou dont need to know about
else if(Health>0)
agent.Stop(true);
if((transform.position - target.position).sqrMagnitude < ShootDistance&&pause.paused == false){
Vector3 toTarget = target.position - transform.position;
toTarget.y = 0;
transform.forward = Vector3.Slerp(transform.forward, toTarget, RotationSpeed * Time.deltaTime);
if(reloadNeeded == false){
reloadNeeded = true;
gameObject.audio.Play ();
Rigidbody shot = Instantiate (projectile, Shooter.position, Shooter.rotation) as Rigidbody;
for(int i = 0; i<shot.GetComponent<GunScript>().Projectiles.Length;i++){
shot.GetComponent<GunScript>().Projectiles[i].GetComponent<DestroyPrefab>().g = gameObject.tag;
shot.GetComponent<GunScript>().Projectiles[i].AddForce (shot.GetComponent<GunScript>().Projectiles[i].transform.forward * shootForce);
}
}
}
}
else
agent.Stop(false);
if (reloadNeeded&&stop == false) {
StartCoroutine(Reload());
stop = true;
}
}
}
Impossible to know what your problem is. Care to share some code?
Did you delete then tag and then rewrite it? You might have put a blank space at the end of your tag.
That script contains more than one reference to Core in it. I can see at least one more in the video.
What happens if you change all references from Core to Enemy.
Does it still work or does that make the Enemy tag fail as well?
hmm, i discovered that it actually sets the destination to Core, but it doesnt move towards it
Answer by Dragon-Of-War · Dec 29, 2014 at 03:39 PM
And as always, the solution is to remake the script. I discovered, while i was remaking the script, that the:
else
agent.Stop(false);
at line 131, i wasnt asking if healing = false, so he will stop everytime if theres no target. Problem solved!
Your answer
Follow this Question
Related Questions
A node in a childnode? 1 Answer
Scene view 3D widget messed up 1 Answer
Weird shadows when baking. 1 Answer