- Home /
Question by
soeiromass · Jun 01, 2016 at 07:23 AM ·
c#unity 5gameobjectraycasting
Unity - Remove GameObject
I have been developing a game in which we need to be able to select a GameObject (ingredient) and deselect it, and that by selecting that GameObject it adds a +1 to the counter. My objective is to use the raycast, within the GameObject "crossair", to select an ingredient and erase it from the scene.
I'm able to do that but only with the wrong ingredients (the ones that the player should not select). I don't understand why it doesn't work with the right ones.
Hope you can help.
My Script in Aim: AimManager.cs
using UnityEngine;
using System.Collections;
public class AimManager : MonoBehaviour {
float TimeInterval = 8f;
float time = 0;
public UI_cozinha uiCozinha;
public AimDrag aimDrag;
public GameObject MainAim, FirstLogin, SecondLogin, ThirdLogin, FourthLogin, CheckLogin, GreenAim;
public RaycastHit hit;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (Physics.Raycast(transform.position, transform.forward, out hit))
{
if (time >= (TimeInterval / 8) && ( ( hit.transform.tag == "ButtonE") || (hit.transform.tag == "ButtonM") || (hit.transform.tag == "ButtonH") ) )
{
MainAim.SetActive(true);
}
if (time >= ((TimeInterval / 8) * 2) && ((hit.transform.tag == "ButtonE") || (hit.transform.tag == "ButtonM") || (hit.transform.tag == "ButtonH")))
{
FirstLogin.SetActive(true);
MainAim.SetActive(false);
}
if (time >= ((TimeInterval / 8) * 3) && ((hit.transform.tag == "ButtonE") || (hit.transform.tag == "ButtonM") || (hit.transform.tag == "ButtonH")))
{
SecondLogin.SetActive(true);
FirstLogin.SetActive(false);
}
if (time >= ((TimeInterval / 8) * 4) && ((hit.transform.tag == "ButtonE") || (hit.transform.tag == "ButtonM") || (hit.transform.tag == "ButtonH")))
{
ThirdLogin.SetActive(true);
SecondLogin.SetActive(false);
}
if (time >= ((TimeInterval / 8) * 5) && ((hit.transform.tag == "ButtonE") || (hit.transform.tag == "ButtonM") || (hit.transform.tag == "ButtonH")))
{
FourthLogin.SetActive(true);
ThirdLogin.SetActive(false);
}
if (time >= ((TimeInterval / 8) * 6) && ((hit.transform.tag == "ButtonE") || (hit.transform.tag == "ButtonM") || (hit.transform.tag == "ButtonH")))
{
CheckLogin.SetActive(true);
FourthLogin.SetActive(false);
}
if (time >= ((TimeInterval / 8) * 7) && ((hit.transform.tag == "ButtonE") || (hit.transform.tag == "ButtonM") || (hit.transform.tag == "ButtonH")))
{
GreenAim.SetActive(true);
CheckLogin.SetActive(false);
}
if (time >= ((TimeInterval / 8) * 8) && ((hit.transform.tag == "ButtonE") || (hit.transform.tag == "ButtonM") || (hit.transform.tag == "ButtonH")))
{
MainAim.SetActive(true);
GreenAim.SetActive(false);
}
if (time >= TimeInterval)
{
if((hit.transform.tag == "ButtonE"))
{
uiCozinha.setDiff(5);
}if((hit.transform.tag == "ButtonM"))
{
uiCozinha.setDiff(7);
}
if ((hit.transform.tag == "ButtonH"))
{
uiCozinha.setDiff(10);
}
aimDrag.getReady(true);
time = 0;
}
else
{
time += Time.deltaTime;
}
}
}
}
My Script in Aim: AimDrag.cs
using UnityEngine;
using System.Collections;
public class AimDrag : MonoBehaviour {
public int erros, certos, totalDeErros, i;
float TimeInterval = 8f;
float time = 0;
public bool StartNextOfMenu;
public RaycastHit hit;
public GameObject easy, medium;
public GameObject farinha, fermento, ovos, leite, acucar;
public GameObject agua, natas;
public GameObject manteiga, oleo, bolachas;
public GameObject[] ArrayEasy = new GameObject[5];
public GameObject[] ArrayMedium = new GameObject[2];
public GameObject[] ArrayDificult = new GameObject[3];
public GameObject[] Nome = new GameObject[15]; // so ta a guardar na primeira posição
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (StartNextOfMenu)
{
Debug.Log(certos);
Debug.Log(erros);
if ( Physics.Raycast(transform.position, transform.forward, out hit) )
{
if (!easy.activeInHierarchy) // então esta no modo facil
{
for (i = 0; hit.transform.tag == "ComidasCertas" && time >= TimeInterval; i++) // Array.length
{
Nome[i] = hit.transform.gameObject;
if (Nome[i] == farinha || Nome[i] == fermento || Nome[i] == ovos || Nome[i] == leite || Nome[i] == acucar)
{
certos++;
if(Nome[i] == farinha)
{
print("nice!");
farinha.SetActive(false);
}
if(Nome[i] == fermento)
{
print("nice1!");
fermento.SetActive(false);
}
if(Nome[i] == ovos)
{
print("nice2!");
ovos.SetActive(false);
}
if(Nome[i] == leite)
{
print("nice2!");
leite.SetActive(false);
}
if(Nome[i] == acucar)
{
print("nice3!");
acucar.SetActive(false);
}
}
else
{
print("teste de erros!");
erros++;
}
time = 0;
}
}
if (!medium.activeInHierarchy) // então esta no modo medio
{
for (i = 0; hit.transform.tag == "ComidasCertas" && time >= TimeInterval; i++)
{
Nome[i] = hit.transform.gameObject;
if (Nome[i] == agua || Nome[i] == natas)
{
print("nice! m");
certos++;
if(Nome[i] == agua)
{
print("nice1! m1");
agua.SetActive(false);
}
if(Nome[i] == natas)
{
print("nice1! m2");
natas.SetActive(false);
}
}
else
{
print("teste erros 2!");
erros++;
}
time = 0;
}
}
if (medium.activeInHierarchy && easy.activeInHierarchy) // esta no modo dificil
{
for (i = 0; hit.transform.tag == "ComidasCertas" && time >= TimeInterval; i++)
{
Nome[i] = hit.transform.gameObject;
if (Nome[i] == manteiga || Nome[i] == oleo || Nome[i] == bolachas)
{
print("nice1! d");
certos++;
if(Nome[i] == manteiga)
{
print("nice1! d1");
manteiga.SetActive(false);
}
if(Nome[i] == oleo)
{
print("nice1! d2");
oleo.SetActive(false);
}
if(Nome[i] == ArrayEasy[2])
{
print("nice1! d3");
bolachas.SetActive(false);
}
}
else
{
print("teste erros 3");
erros++;
}
time = 0;
}
}
if (hit.transform.tag == "ComidasEnradas" && time >= TimeInterval)
{
Nome[14] = hit.transform.gameObject;
erros++;
Nome[14].SetActive(false);
time = 0;
}
if (hit.transform.tag == "ComidasCertas" && time >= TimeInterval)
{
Nome[14] = hit.transform.gameObject;
erros++;
Nome[14].SetActive(false);
time = 0;
}
else
{
time += Time.deltaTime;
}
totalDeErros = certos - i + erros;
}
}
}
public bool getReady(bool confirmation)
{
if (confirmation)
{
return StartNextOfMenu = true;
}
else
{
return StartNextOfMenu = false;
}
}
}
ee4678f93944195857f94d384febce33.png
(308.4 kB)
Comment