Script to affect specific instantiated prefab.

I think I’m just making myself go crazy here. And I’m quite sure there are a lot of simple solutions to this, but I might have confused myself.

    public virtual void AnimationVFX()
    {
        var attackVfx = Instantiate(maData.vfx[attackCounter], transform.GetChild(0).position, this.transform.rotation) as GameObject;
        attackVfx.transform.parent = gameObject.transform.GetChild(0).gameObject.transform;
        Destroy(attackVfx, 1.5f);
    }

I’m currently calling this as an animation event. It works perfectly.
On this prefab that I’m instantiating, I’ve got a visual effect graph.
I need to send it an event to stop.
I’m using the reference documents for this, and it’s also working. Almost.

    public virtual void AnimationVFXOnStop(int whichVfxToStop)
    {
        VisualEffect[] boom = GetComponentsInChildren<VisualEffect>();
    
        boom[whichVfxToStop].SendEvent("OnStop");
    }

This was my atempt at making this work. It’s also called in an animation event. And it kind of works, but not really.
The whichVfxToStop int is there to help me be able to turn off specific visual effects on a single prefab, if I run multiple on the same one.

BUT, the issue now is that if I instantiate multiple prefabs, it can’t differentiate between them when calling the OnStop.

For example, if I give the input to instantiate twice quickly, it only sends the onstop event to first prefab twice, since the first prefab hasn’t been destroyed yet. So I need to be able to differentiate between specific instances.

How could I go about this?

First, stop being mean to yourself.

If you have more than one or two dots (.) in a single statement, you’re just being mean to yourself.

How to break down hairy lines of code:

http://plbm.com/?p=248

Break it up, practice social distancing in your code, one thing per line please.

“Programming is hard enough without making it harder for ourselves.” - angrypenguin on Unity3D forums

Once you fix that tangled dot mess, here’s how to reason about your problem:

You must find a way to get the information you need in order to reason about what the problem is.

What is often happening in these cases is one of the following:

  • the code you think is executing is not actually executing at all
  • the code is executing far EARLIER or LATER than you think
  • the code is executing far LESS OFTEN than you think
  • the code is executing far MORE OFTEN than you think
  • the code is executing on another GameObject than you think it is
  • you’re getting an error or warning and you haven’t noticed it in the console window

To help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

Doing this should help you answer these types of questions:

  • is this code even running? which parts are running? how often does it run? what order does it run in?
  • what are the values of the variables involved? Are they initialized? Are the values reasonable?
  • are you meeting ALL the requirements to receive callbacks such as triggers / colliders (review the documentation)

Knowing this information will help you reason about the behavior you are seeing.

If your problem would benefit from in-scene or in-game visualization, Debug.DrawRay() or Debug.DrawLine() can help you visualize things like rays (used in raycasting) or distances.

You can also call Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene manually, looking for all the parts, where they are, what scripts are on them, etc.

You can also call GameObject.CreatePrimitive() to emplace debug-marker-ish objects in the scene at runtime.

You could also just display various important quantities in UI Text elements to watch them change as you play the game.

If you are running a mobile device you can also view the console output. Google for how on your particular mobile target, such as this answer or iOS: https://discussions.unity.com/t/700551 or this answer for Android: https://discussions.unity.com/t/699654

Another useful approach is to temporarily strip out everything besides what is necessary to prove your issue. This can simplify and isolate compounding effects of other items in your scene or prefab.

Here’s an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong:

https://discussions.unity.com/t/839300/3

Thank you. This is a huge help for my programing knowledge, but I don’t Think it’s party of my question. >.<

The piece of code you’re quoting is simply putting the instantiated prefab as a child to an empty child object one the gameobject that contains the script.

This is on a weapon gameobject, which has a weapon script which the first snippet was from.
The weapon has an empty child called vfx. This is where I put the instantiated gameobjects.

I do understand what the code I wrote does. And I do understand what it isn’t doing.
I just don’t know what approach would actually work here to make it do what I need from it.

The instantiated prefab is destroyed after 1.5f. This means if I perform the input quickly enough, I’ll instantiate several prefabs before they start getting destroyed. Currently, my OnStop code - as is, will only affect the top clone in the hierarchy when it’s called. Since it doesn’t have a way to differentiate betwee the prefabs.
I made the function simply call onstop on every prefab in the child object. That didn’t do the job either, it just created the same issue but in reverse.

I’m currently experimenting with a list, but it’s still not letting me differentiate between the clones…
Next attempt will be try to simply put a bool onto the prefab, and setting it to true during the animation event. Maybe use a for each loop to only send OnStop to the relevant prefab… Somehow…
Like I said. I’m not sure… I feel like this should have been a common question, but couldn’t find anything on it on google or the forum search… So either, the answer is so simple nobody bothered to ask about it… Or I’m imagining something that’s not really possible… XD

Sure… I guess. Why not put a float and an int on there too while you’re at it!

My “next attempt” would be to first try and find out exactly what the code above is doing by lacing that thing with so many Debug.Log() statements that it can barely stand up under its own weight.

But hey, it is your show!