Component script not working

I have a prefab that is instantiated during the game. There are two scripts on the prefab that control its response to mouse over, clicks, etc.

Whenever there is only one instance of the prefab, it behaves as expected. Whenever there is more than one instance, only one of them works.

While the scripts are (to the best of my knowledge) already enabled by default when the prefab is instantiated, I have also added code to enable the script components.

I’m using log statements to verify that the scripts are enabled at the time the associated behavior should be present.

Using C#, new to Unity, but have a nearly (logically) identical prefab for which all instances behave as expected.

Any guidance would be greatly appreciated. Thanks!

Hi there!

Interesting problem. First of all, the components will be enabled or disabled depending on what the prefabs components are set to. So, if you click on your lovely little prefab in the Project window, you can see all of its components in the Inspector window. If they are enabled there, you should be good. I doubt it is an issue with the components being enabled or not.

Um, so… I’m guessing it is an issue with the code itself. It would depend on what the components are actually doing? And it’s possible that all instances of the prefab are actually doing what they are told, and it might just be not what you intended. Yeah, without seeing the code itself, it’s hard to guess why that may be happening. It is just the mouse clicks for example on both prefabs that doesn’t work? Could you give me an explanation of what is exactly happening when the two instances of the prefab is created and it doesn’t work?

Thanks for the response!

Context: I’m making a 2D ccg in which players play cards from their hands onto different “zones” on the board. The issue in question is surrounding the functioning of a “LegalTarget” script on particular zones, which themselves are created (in this case) by instantiating a “universe” prefab.

//(iterating a list of all universes on the board)

        foreach (GameObject zone in universes)
        {
            Debug.Log("universe: " + zone.transform.Find("GUID").GetComponent<Text>().text);

            zone.transform.GetComponent<Outline>().enabled = true;
            zone.transform.GetComponent<LegalTarget>().enabled = true;
            zone.transform.GetComponent<Outline>().effectColor = Game.RED;

            Debug.Log("universe: legalTarget enabled: " + zone.transform.GetComponent<LegalTarget>().enabled);
        }

The above loop correctly displays the GUID of every universe and correctly turns each of them red. It also reports that legalTarget is, in fact, enabled on each.

public class LegalTarget : MonoBehaviour, IPointerEnterHandler
{
     void IPointerEnterHandler.OnPointerEnter(PointerEventData eventData)
    {
        Debug.Log("legalTarget pointer enter");
    
        GetComponent<Outline>().effectColor = Game.YELLOW;


        if (GetComponent<OnClickCardInHand>())
            GetComponent<OnClickCardInHand>().enabled = true;
       else if (GetComponent<OnClickZone>())
           GetComponent<OnClickZone>().enabled = true;
    }    
}

None of the above occurs for the universe zones in question (again, LegalTarget will always work for exactly one universe zone, so if there are n universe zones in play, LegalTarget will not work for n-1 of them).

What confuses me most about this is that I have LegalTarget working as expected on other zones and GameObjects in different scenarios, and I can’t figure out what’s different about this situation (I’ve assumed it has something to do with the prefab).