Hi everyone!
I’m making a game where I have several objects, and I’m completely new to C# and unity.
What I’d like to do is if I select an object with the mouse, I can press a key to hide it.
The problem is that some of my meshes are separated into several meshes (too many vertices). These meshes are the children of a GameObject that doesn’t have a renderer.
I tried two ways to do this, but none works :
-
First try : a script attached to the camera with raycast, it works but only detects the partial meshes and when I press H it hides only the clicked child. I tried but failed making a function searching for siblings to hide them at the same time
-
Second try : a script attached to each object, or partial mesh if the object is split into children meshes. I put the key input into the update function, but when I do that, my list is back to empty, so the key just hides one of the partial meshes. How can I put the List gotten from the OnMouseUp function into the update to make the key hide all the objects from it?
Or is there a simpler solution?
Thank you in advance
I hope my question is understandable…
First try script :
(this one is also changing the shader of the clicked object, but still it is just the partial mesh…)
using UnityEngine;
using System.Collections;
public class SelectionObjetSouris : MonoBehaviour {
public GameObject ObjetClicked = null;
void Update ()
{
if(Input.GetMouseButtonUp(0))
{
Ray ray = GetComponent<Camera>().ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit))
{
if (ObjetClicked == null)
{
ObjetClicked = hit.collider.transform.gameObject;
for (int i = 0; i < ObjetClicked.GetComponent<Renderer>().materials.Length; i++)
ObjetClicked.GetComponent<Renderer>().materials*.shader = Shader.Find("Legacy Shaders/VertexLit");*
}
else
{
for (int i = 0; i < ObjetClicked.GetComponent().materials.Length; i++)
ObjetClicked.GetComponent().materials*.shader = Shader.Find(“Standard”);*
NomObjet = hit.transform.gameObject.name;
ObjetClicked = hit.collider.transform.gameObject;
for (int i = 0; i < ObjetClicked.GetComponent().materials.Length; i++)
ObjetClicked.GetComponent().materials*.shader = Shader.Find(“Legacy Shaders/VertexLit”);*
}
}
}
if (Input.GetKeyDown(KeyCode.H))
ObjetClicked.SetActive(false);
* }*
}
Second try script :
(I manually drag the siblings in the script attached to a partial mesh in unity by chosing the size of “Enfants” if there are children meshes, if there are no partial mesh, I just set “Enfants.size” to 0)
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class GroupeSelectionMesh : MonoBehaviour {
public GameObject[] Enfants;
private int NbEnfants;
private List ObjetActif = new List();
* void OnMouseUp ()*
{
if (Enfants.Length == 0)
{
ObjetActif.Add(gameObject);
}
else
{
NbEnfants = Enfants.Length;
for (int i = 0; i < NbEnfants; i++)
ObjetActif.Add(Enfants*);*
}
}
void Update()
{
if (Input.GetKeyUp(KeyCode.H))
{
foreach (GameObject item in ObjetActif)
{
gameObject.SetActive(false);
}
}
}
}
,