1

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);
    }

}
6
  • the other game object that wasn't selected what do you mean by that? Can you please show us your code? I don't really understand what exactly youw ant to achieve Commented Oct 22, 2018 at 13:27
  • set active to false on the other one, too Commented Oct 22, 2018 at 17:34
  • gyazo.com/699230cd435a48b0963cb8c4771c1ffc so when I take the green apple the yellow apple should go away as well Commented Oct 22, 2018 at 19:36
  • What are the positions of the apples? Commented Oct 23, 2018 at 14:42
  • Yellow Apple is X:-13.86 Y:5.36 Z:-0.3812452 Green Apple is X:12.79 Y:5.16 Z:-03812452 and this is a picture of the spawn points: gyazo.com/d6f4849b6827c948c82cd18b33535f9f also, I made sure the hitboxes don't collide Commented Oct 23, 2018 at 21:34

1 Answer 1

1

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);
    }

}
Sign up to request clarification or add additional context in comments.

5 Comments

I am working in unity 2D and the location in the second suggestion is underlined in red and says. transform does not contain a definition for location and no accessible extension method location accepting first argument type of Transform could be found(are you missing a directive or an assembly reference)
I think he meant transform.position, there is no such thing as transform.location for unity. Try the transform.position and tell me if it works.
the code doesn't error but the second apple doesn't disappear after I take the first.
Katada, Yes, I meant transform.position. thanks. @SIGMA are you putting script this on both objects?
the second one since I will have multiple good and bad items in one scene

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.