I have problem with destroy gameObject, I used this script (updated):
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class pickupJewel : MonoBehaviour
{
public GameObject collectTextObj, intText;
public AudioSource pickupSound, ambianceLayer1, ambianceLayer2, ambianceLayer3, ambianceLayer4, ambianceLayer5;
public bool interactable;
public static int jewelsCollected;
public Text collectText;
private GameObject currentJewel;
public void SetCurrentJewel(GameObject jewel)
{
currentJewel = jewel;
}
void OnTriggerStay(Collider other)
{
if (other.CompareTag("Jewel"))
{
intText.SetActive(true);
interactable = true;
}
}
void OnTriggerExit(Collider other)
{
if (other.CompareTag("Jewel"))
{
intText.SetActive(false);
interactable = false;
}
}
void Update()
{
if (interactable == true)
{
if (Input.GetMouseButtonDown(0))
{
jewelsCollected = jewelsCollected + 1;
collectText.text = jewelsCollected + "/5 jewels";
collectTextObj.SetActive(true);
pickupSound.Play();
Destroy(currentJewel);
if (jewelsCollected == 1)
{
ambianceLayer1.Play();
}
if (jewelsCollected == 2)
{
ambianceLayer2.Play();
}
if (jewelsCollected == 3)
{
ambianceLayer3.Play();
}
if (jewelsCollected == 4)
{
ambianceLayer4.Play();
}
if (jewelsCollected == 5)
{
ambianceLayer5.Play();
{
intText.SetActive(false);
interactable = false;
}
}
}
}
}
}
I want item to be destroyed when a player picks up it.
I think you confused the Unity forum for chatGTP
2 Likes
Excellent idea. Here’s how you can get started right now:
Sounds like you wrote a bug… and that means… time to start debugging!
By debugging you can find out exactly what your program is doing so you can fix it.
Use the above techniques to get the information you need in order to reason about what the problem is.
You can also use Debug.Log(...);
statements to find out if any of your code is even running. Don’t assume it is.
Once you understand what the problem is, you may begin to reason about a solution to the problem.
Remember with Unity the code is only a tiny fraction of the problem space. Everything asset- and scene- wise must also be set up correctly to match the associated code and its assumptions.
If you want actual help, try this format:
How to report your problem productively in the Unity3D forums:
http://plbm.com/?p=220
This is the bare minimum of information to report:
- what you want
- what you tried
- what you expected to happen
- what actually happened, log output, variable values, and especially any errors you see
- links to actual Unity3D documentation you used to cross-check your work (CRITICAL!!!)
The purpose of YOU providing links is to make our job easier, while simultaneously showing us that you actually put effort into the process. If you haven’t put effort into finding the documentation, why should we bother putting effort into replying?
If you post code, only post the relevant code and always use the format button above. Do not post photographs of code.
I fixed this:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class pickupJewel : MonoBehaviour
{
public GameObject collectTextObj, intText;
public AudioSource pickupSound, ambianceLayer1, ambianceLayer2, ambianceLayer3, ambianceLayer4, ambianceLayer5;
public bool interactable;
public static int jewelsCollected;
public Text collectText;
private GameObject currentJewel;
public void SetCurrentJewel(GameObject jewel)
{
currentJewel = jewel;
}
void OnTriggerStay(Collider other)
{
if (other.CompareTag("Jewel"))
{
intText.SetActive(true);
interactable = true;
currentJewel = other.gameObject;
}
}
void OnTriggerExit(Collider other)
{
if (other.CompareTag("Jewel"))
{
intText.SetActive(false);
interactable = false;
currentJewel = null;
}
}
void Update()
{
if (interactable == true)
{
if (Input.GetMouseButtonDown(0))
{
jewelsCollected = jewelsCollected + 1;
collectText.text = jewelsCollected + "/5 jewels";
collectTextObj.SetActive(true);
pickupSound.Play();
if (currentJewel != null)
{
Destroy(currentJewel);
currentJewel = null;
}
interactable = false;
if (jewelsCollected == 1)
{
ambianceLayer1.Play();
}
if (jewelsCollected == 2)
{
ambianceLayer2.Play();
}
if (jewelsCollected == 3)
{
ambianceLayer3.Play();
}
if (jewelsCollected == 4)
{
ambianceLayer4.Play();
}
if (jewelsCollected == 5)
{
ambianceLayer5.Play();
{
}
}
}
}
}
}