Calling the other object's script does not work.

This is the Bulllet’s code.
Upon collision with an Alien, it calls the alien’s GetShot() method.
All compiles and runs Ok, except the alien’s GetShot() method does not get called
(it has a Debug.Log inside, but does not print anything).

void OnCollisionEnter(Collision collision)
{

    //Debug.Log("Collision");
    

    if (collision.gameObject.name == "fly1(Clone)")  //This is the alien's name 
    {
        //This works
        GenericAlien ga = collision.gameObject.GetComponent<GenericAlien>();
       //This too
        Debug.Log("Calling fly to get shot");
       **//This does not work (it has a Debug.Log inside but does not print it). No errors.** 
        ga.GetShot();
        //This works
        Debug.Log("Destroying myself");
        //This too, bullet gets destroyed
        Destroy(gameObject);
    }

    //Check for a match with the specific tag on any GameObject that collides with your GameObject
    if (collision.gameObject.tag == "MyGameObjectTag")
    {
        //If the GameObject has the same tag as specified, output this message in the console
        Debug.Log("Do something else here");
    }
}

It is unclear if you mean that Calling fly to get shot or a debug.log in ga.GetShot is what you talk about when you say it does not print. Could help if you show the ga.GetShot code.

If Debug.Log(“Calling fly to get shot”); is dispayed, it should call ga.GetShot(); nothing wrong with the code here.

Are there any if statments or something that could prevent your debug.log from printing in the other code?

Have you looked in the inspector to see that your instantiated object has the correct code?

Have you tried using the Debugger?

Attaching the Unity Debugger to your project from Visual Studio, set a breakpoint on the ga.GetShot row. Press play. When visual studio stops your game and highlights the breakpoint, press f11 to step into it and see what happens.

If you’re not familiar with the visual studio debugger and unity, please google it. It is something that is very helpful when you debug this type of code and can save you lots of frustrating debugging hours.

Hello !
Thanks for the reply and the debugging advise.
I got it to work.

The Generic Alien prefab was an ABSTRACT class, something for all Aliens to inherit.
Once I turned into a : Monobehavior it worked. Don’t ask my why, I have no idea ha ha.

So I am done with inheritance I guess. I 'll just put all “common Alien stuff” (like hit points,
shooting style, etc) in ANOTHER script, and have all Aliens run that script too. No need to
have only one script per game object, right ? I don’t know why I had enforced myself to
think “one script per game object”.

Thanks, your reply was very useful.
Dimitris