Why script works same for multiple UI objects?

I’ve used a series of online tutorials to build a PVZ clone, the problem is script for the object card words correctly for one object, but not for the other two. I’m using UI objects, instead of sprite, yes I know this is not an efficient method, but that’s the method used in the tutorials. Even if there’s no solution, could someone explain why this is happening?

Thanks,

Here are the two scripts I’m having the problem with.

ObjectCard Script:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;

public class ObjectCard : MonoBehaviour, IDragHandler, IPointerDownHandler, IPointerUpHandler
{
public GameObject object_Drag;
public GameObject object_Game;
public Canvas canvas;
public bool generate;
public bool charge;
public bool contained;
public int Price;
public Image Cross;
public Image Check;
public Text DisplayText;
private Generator generator;
private GameObject objectDragInstance;
private GameManager gameManager;
private ObjectContainer objectContainer;
public static ObjectCard instance;

private void Awake()
{
   instance = this;
}

public void Start()
{
    gameManager = GameManager.instance;
    generator = Generator.instance;
    objectContainer = ObjectContainer.instance;
}

public void Update()
{
    Generate();
    DisplayText.text = Price.ToString();
}

public void OnDrag(PointerEventData eventData)
{
    if(generate)
    {
        objectDragInstance.transform.position = Input.mousePosition;
    }
} 

public void OnPointerDown(PointerEventData eventData)
{
    if(generate)
    {
        objectDragInstance = Instantiate(object_Drag, canvas.transform); 
        objectDragInstance.transform.position = Input.mousePosition;
        objectDragInstance.GetComponent<ObjectDragging>().card = this;
        gameManager.draggingObject = objectDragInstance;
    }
} 

public void OnPointerUp(PointerEventData eventData)
{
    if(generate)
    {
        gameManager.PlaceObject();
        gameManager.draggingObject = null;
        Destroy(objectDragInstance);
        Charge();
    }
} 

public void Generate()
{
    if(generator.Batteries >= Price)
    {
         Check.enabled = true;
         Cross.enabled = false;
         generate = true;
    }
    else
    {
        Cross.enabled = true;
        Check.enabled = false;
        generate = false;
    }
}

public void Charge()
{
    if(contained)
    {
        generator.Batteries -= Price;
        contained = false;
    }
}

}

ObjectContainer Script:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class ObjectContainer : MonoBehaviour
{
public bool isFull;
public bool Contained = true;
public GameManager gameManager;
public ObjectCard objectCard;
public Image backgroundImage;
public SpawnPoint spawnPoint;
public int objectLayer;
public static ObjectContainer instance;

private void Awake()
{
   instance = this;
}

private void Start()
{
    gameManager = GameManager.instance;
    objectCard = ObjectCard.instance;
}

public void Update()
{
    objectLayer = gameObject.layer;
}

public void OnTriggerEnter2D(Collider2D collision)
{
    if(gameManager.draggingObject != null && isFull == false && collision.gameObject.tag == "Robot")
    {
        gameManager.currentContainer = this.gameObject;
        //[backgroundImage.enabled = true;] Suspending this line of code for Aesthetic purposes.
        objectCard.contained = true;
    }
}

public void OnTriggerExit2D(Collider2D collision)
{
    // Had to block out [gameManager.currentContainer = null;] line, it was causing a null bug in GameManager.
    backgroundImage.enabled = false;
}

}

Don’t know so much about what happen but this is my assumption:

  • while using GameManager, or any imported script, I think it only calls one instance instead each of GameObject instance
  • for example: the GameManager has recognize one instance from GameObject A, then GameObject B also called this instance (maybe the same instance, recognized by the back-end system)
  • so by this, the instance we talked here only references either to GameObject A, or B

While this happens, probably the back-end system can only recognize one-saved GameObject, instead for each script-attached GameObject

I have seen a little bit information, probably you have to register this, reference to the GameObject that script being attached, into the back-end system. I have seen yours already reference this into the instance, but I couldn’t find which instance you referered. The script should recognize one GameObject is running independently.

Probably this could help Implementing a game manager using the Singleton pattern | Unity | by Fernando Alcantara Santana | Nerd For Tech | Medium

Good luck!