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
1
Question by sir_ver · Jul 18, 2019 at 07:03 AM · instantiatebulletcheckagent

How to check who shot whom

I have a game where 25 agents are fighting against 25 other. But I have also a problem that I don't know how to check wich enemy shot the other one. I am instantiating bullets and the bullets have a tag "bullet" for the 25 agents and "counterbullet" tag for the other 25. And also use one bullet script for the 25 agents and one script for the other 25. Do you know how I could do a check?

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

3 Replies

· Add your reply
  • Sort: 
avatar image
4

Answer by Hellium · Jul 18, 2019 at 07:54 AM

How are we supposed to help you if you don't provide your current code?


A simple way to do this is to inject a reference of the shooter into the bullet somehow. I've done this when the bullet is instantiated by the Weapon class which is responsible to shoot. The reference of the Shooter is injected by the PlayerController at Start.

 // PlayerController
 public Weapon Weapon;
 
 private void Start()
 {
     Weapon.Shooter = this;
 }
 
 private void Update()
 {
     if( Input.GetMouseButtonDown(0))
         Weapon.Shoot();
 }
 
 // Weapon
 public PlayeController Shooter { get ; set ;}
 
 public Bullet BulletPrefab;
 
 public void Shoot()
 {
     Bullet bullet = Instantiate( BulletPrefab );
     bullet.Shooter = Shooter;
 }
 
 // Bullet
 public PlayeController Shooter { get ; set ;}
 
 private void OnCollisionEnter( Collision collision )
 {
     PlayerHealth playerHealth = collision.transform.GetComponentInParent<Health>();
     if( playerHealth != null )
     {
         playerHealth.Hurt( damages, Shooter );
     }
 }


 // PlayerHealth
 public void Hurt( int damages, PlayerController shooter )
 {
     Debug.Log("The player " + shooter.name + " has hurt me for " + damages + " HP" ) ;
 }
Comment
Add comment · Show 10 · 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 sir_ver · Jul 24, 2019 at 07:47 AM 0
Share

@Hellium I have the class EnemyController and EnemyBulletController:

 public class EnemyBulletController : $$anonymous$$onoBehaviour {
 
     public float speed;
 
 
     [HideInInspector]
     /// <summary>
     /// The associated agent.
     /// This will be set by the agent script on Initialization. 
     /// Don't need to manually set.
     /// </summary>
     EnemyController agent;
     static Animator anim;
     Game$$anonymous$$anager instanceG$$anonymous$$;
 
 
     private void Awake()
     {
         instanceG$$anonymous$$ = Game$$anonymous$$anager.instance;
         agent = EnemyController.instance;
 
         anim = instanceG$$anonymous$$.player.GetComponent<Animator>();
 
     }
 
     // Update is called once per frame
     void Update()
     {
         transform.Translate(Vector3.forward * speed * Time.deltaTime);
         Destroy(gameObject, 2); //8
     }
 
 
     void OnTriggerEnter(Collider col)
     {
         if (col.gameObject.tag == "counterAgent")
         {
             try
             {
 
                 col.gameObject.GetComponent<PlayerController>().GetGame$$anonymous$$anager().PlayerIsDead();
 
             }
             catch(Exception e)
             {
                 Debug.Log(col.gameObject.name);
             }
         }
 
 
         if (col.gameObject.CompareTag("wall"))
         {
             Destroy(gameObject);
         }
 
        
     }

And this in EnemyController I instantiate the bullet:

         if (timeBtwShots <= 0)
         {
             EnemyBulletController newBullet = 
                 Instantiate(bullet, firePoint.position, firePoint.rotation) 
                 as EnemyBulletController;

             timeBtwShots = startTimeBtwShots;
             newBullet.speed = bulletSpeed;
         }
         else
         {
             timeBtwShots -= Time.deltaTime;
         }

How can I change my code so that I can detect which agent shot the player

avatar image sir_ver sir_ver · Jul 24, 2019 at 07:49 AM 0
Share

I hope you guys can help me. I struggle with this since some days

avatar image Hellium sir_ver · Jul 24, 2019 at 10:03 PM 0
Share

There is something very strange with your code:

  private void Awake()
  {
      instanceG$$anonymous$$ = Game$$anonymous$$anager.instance;
      agent = EnemyController.instance;
 
      anim = instanceG$$anonymous$$.player.GetComponent<Animator>();
  }

Why EnemyController is a Singleton? Don't you have several ennemies?

Try this:

 // EnemyBulletController

  public EnemyController agent;
  static Animator anim;
  Game$$anonymous$$anager instanceG$$anonymous$$;
 
 
  private void Awake()
  {
      instanceG$$anonymous$$ = Game$$anonymous$$anager.instance;
      
      anim = instanceG$$anonymous$$.player.GetComponent<Animator>();
  }

  void OnTriggerEnter(Collider col)
  {
      if (col.gameObject.tag == "counterAgent")
      {
          try
          {
               Debug.Log( "Bullet shot by " agent.name, agent );
              col.gameObject.GetComponent<PlayerController>().GetGame$$anonymous$$anager().PlayerIsDead();
 
          }
          catch(Exception e)
          {
              Debug.Log(col.gameObject.name);
          }
      }
 
 
      if (col.gameObject.CompareTag("wall"))
      {
          Destroy(gameObject);
      }    
  }


      // EnemyController
      if (timeBtwShots <= 0)
      {
          EnemyBulletController newBullet = 
              Instantiate(bullet, firePoint.position, firePoint.rotation) 
              as EnemyBulletController;
          timeBtwShots = startTimeBtwShots;
          newBullet.speed = bulletSpeed;
          newBullet.agent = this;
      }
avatar image sir_ver · Jul 25, 2019 at 07:53 AM 0
Share

@Hellium So I did like you said and it is working. The code I have send you was from the main game where I have a player. I alsoo have a unity project where I train the agents. And the code looks like this now:

 if (col.gameObject.tag == "counterAgent")
 {
     try
     {
         //Debug.Log("Bullet shot by: " + agent.name,agent);
         col.gameObject.GetComponent<CounterEnemyController>().GetGame$$anonymous$$anager().CounterAgentDead(col.gameObject,agent.name);
     }
     catch(Exception e)
     {
         Debug.Log(col.gameObject.name);
     }
 }

And in Game$$anonymous$$anager I do this:

    public void AgentDead(GameObject opponent, string killedBy)  
     {
         if (listOfAgent.Contains(opponent))
         {
             agentsDeadsPerRound++;
             scoreCounterEnemy.IncreaseScore();
 
             opponent.GetComponent<EnemyController>().Done();
 
 
             for (int i = 0; i < listOfCounterAgent.Count; i++)
             {
                 if (listOfAgent[i].name == killedBy)
                 {
                     listOfAgent[i].GetComponent<CounterEnemyController>().AddReward(1.0f);
                     break;
                 }
             }
       }

The code enters the catch block everytime when I use agent.name. I get this output:

 System.NullReferenceException: Object reference not set to an instance of an object
   at EnemyBulletController.OnTriggerEnter (UnityEngine.Collider col) [0x00039]

Do you maybe know why it is like this? And it is not entering this if statement: if (listOfAgent[i].name == killedBy)

Oh sorry my bad, it is entering the if statement. At first I placed a debug.log after the addReward() statement. And there was no output. Then I placed it before the the statement and it worked. It is a strange behaviour.

avatar image Hellium sir_ver · Jul 25, 2019 at 10:06 PM 0
Share

At first I placed a debug.log after the addReward() statement. And there was no output. Then I placed it before the statement and it worked. It is a strange behaviour.

You may have an exception.

  for (int i = 0; i < listOfCounterAgent.Count; i++)
          {
              if (listOfAgent[i].name == killedBy)

It's strange you use listOfCounterAgent.Count as a limit of your loop, but you check against listOfAgent[i].name

avatar image sir_ver · Jul 26, 2019 at 07:04 AM 0
Share

@Hellium Yes you are absolutely right! But I still get the error. Could the null refence appear because I set the EnemyController agent to public and it is awaiting an instance? I also got the error, when I did not pass the agent.name parameter to game$$anonymous$$anager. When I did Debug.Log(agent.name) I also got the null reference exception

Could it be that when the bullet touches the agent it gets destroyed. When it is destroyed it does not have the instance anymore

avatar image Hellium sir_ver · Jul 26, 2019 at 07:17 AM 0
Share

The agent is public indeed but does not need to be set in the inspector. It should be set in the EnemyController when the bullet is instantiated as I have indicates.

    EnemyBulletController newBullet = 
   Instantiate(bullet, firePoint.position, firePoint.rotation) 
   as EnemyBulletController;
   timeBtwShots = startTimeBtwShots;
   newBullet.speed = bulletSpeed;
   newBullet.agent = this; // here



avatar image sir_ver Hellium · Jul 26, 2019 at 07:21 AM 0
Share

hmm I have already done that but still :(

Show more comments
avatar image
0

Answer by DropoutGamer · Jul 18, 2019 at 07:15 AM

I think i got your question , you have 1 script using on 50 objects, in 2 25 25 group with same tag names .. i think you can get name of gameobjects ... i dont it think it matters that all 50 gameobject names are different .. u can simply use a debug to get names of gameobject from which ray cast is initiated and which game object got hit hit.collider.gameObject.name;

Comment
Add comment · Show 2 · 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 sir_ver · Jul 18, 2019 at 07:17 AM 0
Share

So one agent1 hs shot agent2. And I want to check which of the 25 agents shot agent2. Is it still the same then?

avatar image DropoutGamer sir_ver · Jul 18, 2019 at 07:20 AM 1
Share

yeah .. if all 50 agent have different names, u can simply get the names to understand who is killing whom .

avatar image
0

Answer by sir_ver · Jul 26, 2019 at 07:35 AM

@Hellium This is the EnemyBulletController code:

 using UnityEngine;
 using System.Collections;
 using System;
 
 public class EnemyBulletController : MonoBehaviour {
 
     public float speed;
 
     [HideInInspector]
     public EnemyController agent;
 
     static Animator anim;
     GameManager instanceGM;
 
 
     private void Awake()
     {
         instanceGM = GameManager.instance;
         //agent = EnemyController.instance;
 
         anim = instanceGM.player.GetComponent<Animator>();
 
     }
 
     // Update is called once per frame
     void Update()
     {
         transform.Translate(Vector3.forward * speed * Time.deltaTime);
         //Destroy(gameObject, 2); //8
     }
 
 
     void OnTriggerEnter(Collider col)
     {
         if (col.gameObject.tag == "counterAgent")
         {
             try
             {
                 //Debug.Log("Bullet shot by: " + agent.name);
                 col.gameObject.GetComponent<CounterEnemyController>().GetGameManager().CounterAgentDead(col.gameObject,agent.name);
                 Destroy(gameObject);
             }
             catch(Exception e)
             {
                 Debug.Log(e);
             }
         }
 
 
         if (col.gameObject.CompareTag("wall"))
         {
             Destroy(gameObject);
         }
        
     }
 
 
 }


And this is the CounterEnemyBulletController:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using System;
 
 public class CounterEnemyBulletController : MonoBehaviour {
     public float speed;
 
 
     [HideInInspector]
     public CounterEnemyController agent;
 
     static Animator anim;
     GameManager instanceGM;
 
 
     private void Awake()
     {
         instanceGM = GameManager.instance;
         //agent = CounterEnemyController.instance;
 
         anim = instanceGM.player.GetComponent<Animator>();
 
     }
 
     // Update is called once per frame
     void Update()
     {
         transform.Translate(Vector3.forward * speed * Time.deltaTime);
         Destroy(gameObject, 2); //8
     }
 
 
     void OnTriggerEnter(Collider col)
     {
 
         if (col.gameObject.tag == "agent")
         {
             try
             {
                 //Debug.Log("Bullet shot by: " + agent.name);
                 col.gameObject.GetComponent<EnemyController>().GetGameManager().AgentDead(col.gameObject,agent.name);
 
             }
             catch(Exception e)
             {
                 Debug.Log(e);
             }
         }
             
 
        
         if (col.gameObject.CompareTag("wall"))
         {
             Destroy(gameObject);
         }
 
     }
 
 
 }


Comment
Add comment · Show 4 · 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 Hellium · Jul 26, 2019 at 07:53 AM 0
Share

What is this CounterEnemyBulletController class? It is almost identical to EnemyBulletController. Are you sure it is instantiated the same way as a EnemyBulletController and that the agent is correctly set? I don't believe you should have two classes with so much in common.

avatar image sir_ver Hellium · Jul 26, 2019 at 07:58 AM 0
Share

I have two types of agents: One is CounterEnemy and the other one is just Enemy. So I created then the scripts for both of the type of agents to differentiate the shooting and movement etc. I am using the ml agents toolkit, where I have to train the agents. Later on I will replace the counterEnemy with the Player and the player will be played by the user.

avatar image sir_ver Hellium · Jul 26, 2019 at 08:23 AM 0
Share

Last chance: $$anonymous$$aybe because this GetGame$$anonymous$$anager() ? This also returns an instance like that:

 public Game$$anonymous$$anager GetGame$$anonymous$$anager()
 {
     return instanceG$$anonymous$$;
 }
avatar image Hellium sir_ver · Jul 26, 2019 at 10:05 PM 0
Share

I really don't know... But this error falls outside the scope of this question (and outside the scope of Unity Answers). Use the debugger of your IDE to understand what's going on and make sure all your variables are correctly initialized.

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

133 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 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

Add force to Instantiated object. 4 Answers

GameObject Instantiated in Wrong Position 1 Answer

Modify the rotation of Instatiate prefab 1 Answer

problem with instantiate function 2 Answers

Randomly instantiate bullets within a certain angle based on look direction 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