Wrong for grenade

Hello, I had a problem after this tutorial, I completely copied everything you did in the lesson, but in the end 2 problems: the first problem, after each grenade throw, I get a “null reference exception” in the line onGrenadeBehaviorComplete(); in the GrenadeProjectile script, the second problem is that the throw is backwards,
one unit shoots just backwards, the other unit shoots backwards and hits itself and dies, on your video the shooting takes place at the place you specified


Let’s fix the first issue first: Have you assigned anything to onGrenadeBehaviorComplete? I can see it is being set in the Setup() function, but did you pass anything into that function when you set it up?

Also, see here on how to post code: How to apply code formatting within your post

My code is 100% identical to the author’s code, including the names of the fields and functions, I didn’t add or change anything, because I don’t understand English, I don’t know what the author says, and I have to copy everything to understand how the code works…but now I I don’t understand what I’m missing and why it works for the author and I don’t …
I checked the scripts several times and didn’t find any difference

Have you passed anything into the Setup() function? Can you show me the code for the GrenadeAction

My script is the same as that of the author, I only wrote what he wrote, here are the screenshots:

now I started the next lesson, which is also related to grenades, and now nothing just works for me)



I see a big difference between the grenade script and the shooting script, I understand that something is missing, but I don’t understand what exactly and where it should be

Your problem is here:

It should be

public override void TakeAction(GridPosition gridPosition, Action onActionComplete)
{
    Transform grenadeProjectileTransform = Instantiate(grenadeProjectilePrefab, unit.GetWorldPosition(), Quaternion.identity);
    // Get the component from the INSTANTIATED object, not the PREFAB
    GrenadeProjectile grenadeProjectile = grenadeProjectileTransform.GetComponent<GrenadeProjectile>();
    grenadeProjectile.Setup(gridPosition, OnGrenadeBehaviorComplete);
    ActionStart(onActionComplete);
}

This should solve both your issues because your projectile had no ‘complete’ callback (null reference exception) and no target, so all grenades were being thrown at (0,0,0)

1 Like

omg…really, such a stupid mistake, I checked several times and did not notice…sorry for my inattention and thanks a lot for the help, it really solved the problem…there is only one nuance, in the area where I point there is an explosion ,particle system, but no grenades,
it looks like it is destroyed immediately upon spawning when it hits the collider of the unit that shoots), but in any case, the main logic is already working and it’s good,thanks again

The grenade doesn’t get destroyed when it hits a collider. It gets destroyed when it reaches the target. What does your prefab look like? The grenade game object reaches the target. That’s why the particles are where it’s meant to be. It doesn’t get destroyed because then the particles wouldn’t happen either

Yes, I have a strange situation, in fact, the grenade appears and is not destroyed as I thought, it remains at the feet of the unit that attacks, the grenade is small, so I did not immediately see it …in general, it spawns, remains at the feet, visual explosion and damage occur in the right place…this is very strange, the logic is the same as that of a bullet, but the bullet flies and there is no grenade), I also wanted to ask for advice on how to make delays in the code when applying skills? Because I use animations for every action and animations don’t keep up with the code, this is especially noticeable with a grenade throw…I tried to create different coroutines with different conditions, but it doesn’t work, empty coroutines in the middle of a function with an action don’t work either, the function is executed all at once even if there is a coroutine with a wait of a few seconds…the only working solution I could find is to speed up animations 3-4 times, but it looks ugly

If you show the grenade prefab, we may be able to figure out what’s wrong. I suspect you may have the GrenadeProjectile component on a child object instead of the prefab root

As for the other issue: functions don’t wait for coroutines to finish before continuing so it won’t work. Timing scripts with animations is difficult and not always a good idea, and it also depends on what you are doing. In some cases you could use animation events to have certain things happen at certain points during the animation. For example, if you have a throw animation and want the grenade to only start flying when the hand reaches a specific position, you’d set up an animation event at that point in the timeline and spawn the grenade when the event is triggered.

You can find more info on Animation Events here

With regards to the grenade, here is my prefab, everything seems to be correct, the only difference from the author is in the grenade model, instead of a sphere I use a grenade model, there is only a Mesh in it, in the objects nested in it there is also only a Mesh

Regarding animation events, I know them and have already used them many times, but your code structure is still difficult for me,
and I don’t understand how to correctly break a function into several functions and at the same time call it from an animation, it looks too complicated, I thought maybe with the help of code you can make delays or code to break the function into several parts … if I understand the code correctly,
the action activates the UnitActionSystem script in the HandleSelectedAction function when the left mouse button is pressed

UnitActionSystem calls TakeAction, but in Grenade I just need to call the animation in the TakeAction method, all the rest of the logic needs to be removed in another method and this method should be called through the event in the animation. Right?

The grenade prefab looks fine, but I can see everything. Does the model perhaps have a rigidbody?

Your animation event solution sounds correct: Trigger the animation in TakeAction and let the animation spawn the grenade when the event triggers.

If you want, you can zip and upload your project somewhere, and I could take a look at the grenade and see why it’s not working properly

Thanks for the help, I was able to set up the events in the animation correctly and now it looks much better. As for the grenade, there is definitely nothing but Mesh and only the script from the parent, I checked both the prefab and also checked it in the game itself. It looks like some value is zero, usually when the speed is 0 the object is hanging in place,
but I checked through the debug, it showed me the speed which is 15, maybe the problem is in some other variable, I will try to find the problem myself and solve it, if I fail, then I can show the project to you. Thanks again for your help, now I know how Actions work and I can correct the game

Privacy & Terms