Search Unity

Basic Question: Making one object trigger a non-static method in multiple prefab Instances

Discussion in 'Prefabs' started by jf065d, May 14, 2022.

  1. jf065d

    jf065d

    Joined:
    May 28, 2018
    Posts:
    23
    Hi,
    1. I have multiple instances of an enemy prefab in my game.
    2. I have a different object that needs to trigger a method within these instances that checks whether the enemy is moving. If it is, the method will reduce it's speed. If they're not, the method will do nothing.
    Question: Because the enemies are moving at different speeds, I cannot make the speed static within the enemy prefab script, and thus I also cannot make the method static. What is the correct way to use one script, to trigger a non-static method on multiple instances of a prefab?

    I'm new to coding/Unity, so I hope that question makes sense. I can't seem to find documentation on this anywhere so I may either be asking the question wrong, or missing something very obvious.

    Thank you,
    Joe
     
  2. jf065d

    jf065d

    Joined:
    May 28, 2018
    Posts:
    23
    Figured it out. For the object that needed to call the method within my enemy prefab instances, I added a delegate. I then had my prefab subscribe to the delegate and "observe" whenever the other object invoked the change. This video was helpful: