Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by shadowpuppet · Jan 21, 2017 at 12:57 AM · instantiate-game-object

respawning enemies that recognize player

Trying to get more mileage from my prefab enemies. In their ATTACK scripts they seek out the player by his transform - a public variable I drag and drop "player" into and all is well. When I respawn that enemy that field is empty so he ignores me . How rude. Question is what do I need to do to this script so that field isn't empty when the enemy prefab spawns? specifically , the error line is

"if(Vector3.Distance(transform.position,Player.position)

this is where the enemy is suppose to tell if I am close enough to attack

 public Transform Player;

is where I drag and drop the player into the script for the original enemy. but here's the entire script

 using UnityEngine;
 using System.Collections;
 
 public class neoNaziAttack : MonoBehaviour {
 
     public enum EnemyState{IDLE = 0, WALK = 1, RUN = 2, ATTACK = 3, KILL = 4};
     public GameObject enemy;
     public Transform Player;//adds the player to the script
     public int MaxDist= 8;//maximum distance the player can be before enemy stops chasing the player
     //public int NavDist= 8;
     public int NavSpeed= 8;
     public float MinDist= 1;//how close the player can be to enemy for enemy to start chasing player
     public EnemyState currentState;
     public GameObject getHim;
     Animator anim;
     public PlayerHealth playerHealth;
     private NavMeshAgent agent;
     public static bool PlayerDetected ;
     public bool PlayerDetected2 ;
     private bool jumping = false;//************this is the delay from hitreact
     public float timeWhileJumping = 2f;
     private bool jumping2 = false;//*****this is the delay from run to firing gun
     public float timeWhileJumping2 = 2f;
     public MonoBehaviour gun;
     public  GameObject naziWayOff;//turns off waypoints
 
     void HitByRay () {
         naziWayOff.SetActive  (false);
         anim.SetTrigger ("Hit");//ScoreManager.score += scoreValue;
         StartCoroutine(DelayAfterJump());
         agent.enabled = false;
         transform.LookAt(Player);
         anim.ResetTrigger ("walk");
         anim.ResetTrigger ("idle");
         anim.ResetTrigger ("idle2");
         anim.ResetTrigger ("attack");
         jumping = true;
     }
     void Start()
     {
         gun= enemy.GetComponent <naziRifle> ();
         anim = enemy.GetComponent<Animator>();
         agent = enemy.GetComponent<NavMeshAgent>();
         playerHealth = GameObject.FindWithTag("Player").GetComponent <PlayerHealth> ();
         //Player=Transform.FindWithTag("Player");
     }
     void Update()
     {
         PlayerDetected2 = PlayerDetected;
 
             agent.enabled = true;
     //    if (currentState == EnemyState.ATTACK)
             //agent.enabled = false;
         if(PlayerDetected ==true)
         MaxDist= 25;
         if(PlayerDetected ==true)
         naziWayOff.SetActive  (false);
         if(PlayerDetected ==true)
         agent.speed = NavSpeed;
         if(PlayerDetected ==true)
         agent.stoppingDistance = MinDist;
         if(PlayerDetected ==true)
         agent.acceleration = 7;
 ;
             if(playerHealth.currentHealth <= 0)
         anim.SetTrigger ("idle");
 
         if(Vector3.Distance(transform.position,Player.position) <= MaxDist)//if that object has the tag "Player"
         {
             PlayerDetected = true;
             if (PlayerDetected)
             {
 
                 transform.LookAt(Player);//Looks at the player
                 currentState = EnemyState.RUN;
 
 
             }
         }
         else if(Vector3.Distance(transform.position,Player.position) > MaxDist)
         {
             RoamAround ();
         }
         if (Vector3.Distance (transform.position,Player.position) < MinDist)
         {
             
             currentState = EnemyState.ATTACK;
             Attack ();
             
         }
         
         if (currentState == EnemyState.IDLE)
         {
 
             gun.enabled = false;
             RoamAround ();
         }
         else if (currentState == EnemyState.WALK)
         {
             anim.SetTrigger ("walk");
 
         }
         else if (currentState == EnemyState.RUN)
             
         {
             agent.SetDestination(getHim.transform.position);
             agent.enabled = true;
             anim.SetTrigger ("run");
             anim.ResetTrigger ("attack");
             anim.ResetTrigger ("idle");
             gun.enabled = false;
             anim.ResetTrigger ("stand");
         }
         else if (currentState == EnemyState.ATTACK)
         {
             //gun.enabled = true;
 
             anim.ResetTrigger ("run");
 
 
 
         }
         else if (currentState == EnemyState.KILL)
         {
             //anim.Stop ("machinegunblast");
             //GetComponent<Animation>().CrossFade (enemyKill.name);
         }
         
     }
     void RoamAround()//__________________________________________________________________________________________________________________
     {
         gun.enabled = false;
         naziWayOff.SetActive  (true);
 
     }
     public void Attack()
     {
         anim.SetTrigger ("aim");
         agent.enabled = false;
         StartCoroutine(DelayAfterJump2());
         anim.ResetTrigger ("run");
         anim.ResetTrigger ("idle");
         jumping2 = true;
         if(playerHealth.currentHealth <= 0)
             anim.SetTrigger ("idle");
         if(playerHealth.currentHealth <= 0)
             currentState = EnemyState.IDLE;
 
     }
 
 private IEnumerator DelayAfterJump(){
     yield return new WaitForSeconds(timeWhileJumping);
 
         MaxDist= 30;
 
     anim.ResetTrigger ("Hit");
     jumping = false;
         agent.enabled = true;
         agent.SetDestination(getHim.transform.position);
 }
     private IEnumerator DelayAfterJump2(){
         yield return new WaitForSeconds(timeWhileJumping2);
         gun.enabled = true;
         jumping2 = false;
         anim.ResetTrigger ("aim");
     }
 }
 
 
 
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by shadowpuppet · Jan 21, 2017 at 02:06 AM

so simple... remove the "public" option and just assign it in the start function;

 Transform Player;
 
 
 void Start()
     {
     
         Player=GameObject.FindGameObjectWithTag("Player").transform;
     }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

88 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Instantiating game objects - shallow or full copy? 0 Answers

How to instantiate a platform each time another platform is 3 units away from the gamecontroller? 0 Answers

¿como instanciar una plataforma cada vez que otra plataforma este a 3 unidades de distancia del gamecontroller? 0 Answers

[Help] How can I instantiate game objects across a field while some objects are "more rare" than others? 1 Answer

Save a obj and spawn it without make prefape 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges