Disabling 2 objects with 1 mouseclick!

Hey there.I am making a 3d game but i am facing an issue.I have a vase and i want to de-activate it and activate another item when i click the vase(both of them with 1 click). Here is my code but for some reason when i write the code to de-activate the 2nd item, the script won’t work :

using UnityEngine;
using System.Collections;

public class TreasureItemsHandler : MonoBehaviour {

  public GameObject treasureitem;
  //public GameObject floatingisland_1;

  // Disabling the treasureitem when clicked!
  void OnMouseDown()
  {
  treasureitem.SetActive(false);
  //floatingisland_1.SetActive(true);
  }
}

EDIT : The floatingisland item is de-activated in the scene!

Please help me solve this! THanks!:wink:

um…

floatingisland_1.SetActive(true);

you do know that true, activates something, right?

put false there from de-activating.

edit: if you want to de-activate the current object, use gameObject.SetActive(false);

i meant to say in my post that i want to de-activate the vase and activate the other item… should i change my code or nope?That’s why i have it on false

no, dont change your code.

then what shall i do? as it is right now, it does not work!

what are you clicking on? the treasure? or another object, which controls all of this?

The treasure only but it won’t de-activate when i add the code for the activation of the floating island.Otherwise it De-actiavtes itself!

ok, you click on the treasure, which means that your code should look like this…

using UnityEngine;
using System.Collections;
 
public class SwapTo : MonoBehaviour {
    public GameObject swapTo;

    // Disabling the treasureitem when clicked!
    void OnMouseDown()
    {
        gameObject.SetActive(false);
        swapTo.SetActive(true);
    }
}

Thanks so much it worked now :stuck_out_tongue: ! But where was my mistake?Was it that i instead of using gameobject.setactive for the treasure i used treasureitem.setactive… ?

Also can i make them both de-activate and activate slowly? like fade in?Do i need a coroutine ?

Yes u need a coroutine for fading in/out. The docs explain how to to do it.

Ok apreciate it thanks! :smile:

1 Like