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 /
avatar image
0
Question by VinnyG · Oct 04, 2011 at 09:05 PM · c#2daitagstargeting

2d AI: enemy targeting c#

Hi

I'm developing a 2d game where I spawn ally units from left to right. I'm trying to get my enemy units to fire a projectile at the ally units once they come within range on the x axis. My script works fine if I have an ally unit already placed in the scene but if I start the scene without a pre-spawned ally there is an error that says: UnassignedReferenceException: The variable target of 'Enemy' has not been assigned. Here is my enemy script:

void Start () {

   GameObject go = GameObject.FindGameObjectWithTag("AllyTag");
     
   target = go.transform;
     
     
   SetPositionAndSpeed();
 }
 
 // Update is called once per frame
 void Update () {
 
     float amountToMove = currentSpeed * Time.deltaTime; 
     transform.Translate(Vector3.left * amountToMove);
     
     
     StopAndShoot();//if target comes within range
     
      
      
 }
 
 
 
 
 public void SetPositionAndSpeed(){
     
     //getting speed
 currentSpeed = Random.Range(MinSpeed, MaxSpeed);
     
     
 }
 
 
 void StopAndShoot(){
     x = transform.position.x;
     c = target.position.x;
     
     if(c>=x-6){
         
         currentSpeed = 0;//stop
         Instantiate (BulletPrefab,transform.position,Quaternion.Identity);//fire bullet

                 }
  
  }
     

}

If anyone can help it would be great. Regards Vinny

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by kgoswami · Oct 04, 2011 at 10:11 PM

If there is no object int the game scene with "allytag" tag, then the Find will return a NULL. Put an if statement checking fora NULL first. If it is not Null then do ur thing.

Also when u spawn an enemy, check its name in the haeirarchy, it could have a (Clone) attached behind its name. You can check for the name as well and assign it a new name when it is spawned.

Comment
Add comment · Show 1 · 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
avatar image vxssmatty · Oct 04, 2011 at 10:35 PM 1
Share

Null checking is always a good idea; but note this wont help your underlying problem;

You assign the GO to a variable at Start(), so any other time a new ally thing spawns, your enemies wont pick it up.

You would have to loop a co-routine to query the current ally things in scene, and choose one to target onto. You would turn off the loop at this point until the enemy is ready to search for a new ally thing.

$$anonymous$$atty

avatar image
0

Answer by VinnyG · Oct 07, 2011 at 12:01 AM

Thanks for the advice guys. I have it working now using the co-routine method.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Simple AI In 2D - C# 1 Answer

[C#] Raycasts and Object Tags 1 Answer

Basic 2d AI follow? 1 Answer

A* pathfinding for 2D top Down 0 Answers

Check lenght from player? 2 Answers


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