I’m trying to have my instantiated bullet update the score board by telling it who fired the bullet and have it register who it hit.
I got it to work but badly, it doesn’t work everytime and I believe it’s because I’m using gameobject.find to get acces to the health script of the person that is hit by the bullet to check if that person is dead yet or not.
Is there any way to get access to another GO’s components from only a public string i.e. the players name?
This is one way to do it without using gameobject.find, but it doesn’t work every time. It happens inside OnCollisionEnter:
if (collision.gameObject.tag == "Sam")
{
PlayerName p = collision.gameObject.GetComponent<PlayerName>();
myDestroyer = p.playerName;
DidIDie die = collision.gameObject.GetComponent<DidIDie>();
if (die.iDied)
{
Debug.Log("bool CheckDead should become true if the bool 'iDied' is true on the hit object tagged 'Sam'");
Checkdead = true;
}
}
So neither gameobject.find nor collision.gameObject.GetComponent and check a bool works reliably… How do I do this??
Your script is a little confusing and seems like you tried to over complicate things, but it is on the right track. When a bullet hits something, you should be able to get it’s health script or whatever it is you’re using to check to determine if it should be “dead” and it will work just fine.
Unfortunately, I don’t understand what you are actually doing since you have both a tag check for “Sam” and you’re getting a PlayerName and a DidIDie component, but then your Checkdead is being set on whatever object this is.
Either way, not sure if I can explain it much better, but there should be a bunch of tutorials on this sort of thing.
Allow me to explain:
The player tagged “Sam” has a different name that the person playing with the character tagged “Sam” has made up. “myDestroyer” is a public string used in another function to update the scoreboard with the name of the person playing the character tagged “Sam” when that character dies.
I try to check if that character has died with : "
DidIDie die = collision.gameObject.GetComponent();
if (die.iDied)
{
//Activate local bool checkDead which then starts the score updating function.
}
As you mention the bullet should be able to detect if Sam has died on collision no problem. Except it doesn’t do that every single time, sometimes it doesn’t register at all. How would you check the health script on an hobject the bullet collided with? Did I write it wrong maybe?
I’m still not sure if I’m getting it. But I assume the tag “Sam” just means it’s the player?
If your script isn’t running each time, I’d say double check that the collision is even happening.If the script you showed is the bullet, you want to make sure it hits the target correctly. However, I would advice against the bullet doing so much. A bullet, once fired probably only needs three main things. It needs to know who fired it, who it hit, and how much damage it did.
The player script can then take the damage and process if the character is dead and if so, report it to another script that handles all the other stuff.
Now, what I said above is based on the life of a bullet. In a game, a bullet isn’t going to stick around. It needs to do its job and then go away. With this in mind, it’s also possible your collision is happening every time, but there is some disconnect with timing.
So, the first thing to address and figure out is does your collision happen every time and if not, why? Once you narrow that down, I think it will help to start addressing some other issues.
I would suggest you put some debug.log calls in your script to double check when code is or isn’t being executed.
Sorry for the late reply, I did find the reason for why it sometimes worked sometimes didn’t and it was because the bullet that collides with a player makes the player loose x-amount of health and the it checks if the bool iDoneDIed is true at the same time. this cases problems so by doing bool check .5 seconds after the actual collision it works every time now.
here’s the coroutine:
IEnumerator LookDead(Collision coll)
{
yield return new WaitForSeconds(.5f);
if (coll.gameObject.GetComponent<DidIDie>().iDoneDied)
{
checkDead= true;
}
}
And then I simply put StartCoroutine(LookDead(coll)); inthe onCollisionEnter function so that it starts on collision with the player tagged “Sam”. Hope this helps others with the same problem and thank you for your help