Search code examples
c#unity-game-enginegameobject

C# Unity script for item pick up choice


I currently have a script that instantiates 2 prefabs as GameObjects. Which have attached to them a script that the game-object is set active to false, then is added to my inventory array. Which is another script, but my issue is the other game object that wasn't selected is still there. It is supposed to be you have a choice of 2. Once you take one the other disappears. I have no idea how to do this, could anyone please help.

public int moralityCounter=0;
public bool Inventory;

public void Store()
{
    if (gameObject.CompareTag("Good"))
    {
        moralityCounter++;
        Debug.Log(moralityCounter);
        gameObject.SetActive(false);
    }
    if (gameObject.CompareTag("Bad"))
    {
        moralityCounter--;
        Debug.Log(moralityCounter);
        gameObject.SetActive(false);
    }

}

Solution

  • If there's only the single good and single bad tagged objects, you can just set everything tagged good and bad to be inactive.

    private void DeactivateAllGoodBads() 
    {
        // Deactivate all goods
        GameObject[] goodObjects = GameObject.FindGameObjectsWithTag("Good");
        foreach (GameObject goodObject in goodObjects)
        {
            goodObject.SetActive(false);
        }
    
        // Deactivate all bads
        GameObject[] badObjects = GameObject.FindGameObjectsWithTag("Bad");
        foreach (GameObject badObject in badObjects)
        {
            badObject.SetActive(false);
        }
    }
    
    public void Store()
    {
        bool isGood = gameObject.CompareTag("Good");
        bool isBad = gameObject.CompareTag("Bad");
        if (isGood)
        {
            moralityCounter++;
            Debug.Log(moralityCounter);
        }
    
        if (isBad)
        {
            moralityCounter--;
            Debug.Log(moralityCounter);
        }
    
        if (isGood || isBad)
        {
            DeactivateAllGoodBads();
        }  
    }
    

    If there are multiple, you can do something like only disable ones closer to the Stored object than some distance.

    private void DeactivateCloseGoodBads(Vector3 position, float maxDistance)
    {
        // Deactivate close goods
        GameObject[] goodObjects = GameObject.FindGameObjectsWithTag("Good");
        foreach (GameObject goodObject in goodObjects)
        {
            // Check distance of found good
            if (Vector3.Distance(position, goodObject.transform.position) <= maxDistance) {
                goodObject.SetActive(false);
            }
        }
    
        // Deactivate close bads
        GameObject[] badObjects = GameObject.FindGameObjectsWithTag("Bad");
        foreach (GameObject badObject in badObjects)
        {
            // Check distance of found bad
            if (Vector3.Distance(position, badObject.transform.position) <= maxDistance) {
                badObject.SetActive(false);
            }
        }
    }
    
    public void Store()
    {
        bool isGood = gameObject.CompareTag("Good");
        bool isBad = gameObject.CompareTag("Bad");
        if (isGood)
        {
            moralityCounter++;
            Debug.Log(moralityCounter);
        }
    
        if (isBad)
        {
            moralityCounter--;
            Debug.Log(moralityCounter);
        }
    
        if (isGood || isBad) 
        {
            DeactivateCloseGoodBads(gameObject.transform.position, 10f);
        }
    
    }