Hello everybody.
For the moment :
If I right click on a group of objects they are detected by a raycast and memorized on the array “hits”. Their names appear on a “group region”, which can use a vertical scrollbar if there is too many objects.
What I want to do :
If I click on a button with the name of an object I want an “object region” to appear on the right of the group region, with the name of the object in it. But nothing happens 
using UnityEngine;
using System.Collections;
public class selectObject : MonoBehaviour {
// variables of the regions
public float regionX;
public float regionY;
public float regionWidth;
public float regionHeight;
public static Rect groupRegion;
public static Rect objectRegion;
// variables for detection
private RaycastHit hit;
private RaycastHit[] hits;
private Vector3 pos;
private string objectName;
private bool hasHit = false;
private Vector2 scrollPosition;
void Start() {
// position and size of the regions
regionX = Screen.width / 32;
regionY = Screen.height / 16;
regionWidth = 150;
regionHeight = 200;
groupRegion = new Rect(
regionX,
regionY,
regionWidth,
regionHeight
);
objectRegion = new Rect(
regionX + regionWidth,
regionY,
regionWidth,
regionHeight
);
}
void OnGUI () {
// nothing on right click : nothing appears
if(hasHit) {
GUI.BeginGroup(groupRegion);
// scrolling if too many objects
scrollPosition = GUILayout.BeginScrollView(
scrollPosition,
GUILayout.Width (regionWidth),
GUILayout.Height (regionHeight)
);
// size of the scrollbar
GUI.skin.verticalScrollbar.fixedWidth = 20;
GUI.skin.verticalScrollbarThumb.fixedWidth = 20;
// planar coordinates
GUI.skin.box.fontSize = 20;
GUILayout.Box ("("+pos.x+","+pos.y+")");
// list of names
for (int i = 0; i < hits.Length; i++) {
hit = hits *;*
-
objectName = hit.transform.name;*
-
if (GUILayout.Button(objectName)) {*
-
// HERE IS MY QUESTION*
-
GUI.BeginGroup(objectRegion);*
-
GUILayout.BeginVertical("box");*
-
GUILayout.Box (objectName);*
-
GUILayout.EndVertical();*
-
GUI.EndGroup();*
-
}*
-
}*
-
GUILayout.EndScrollView ();*
-
GUI.EndGroup();*
-
} *
-
}*
-
void Update () {*
-
// right click for detection*
-
if (Input.GetMouseButtonDown(1)) {*
-
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);*
-
// memorize objects if hit*
-
if (Physics.Raycast (ray, out hit, 100)) {*
-
hasHit = true;*
-
pos = hit.transform.position ;*
-
hits = Physics.RaycastAll(ray, 100);*
-
} else {*
-
hasHit = false;*
-
}*
-
}*
-
}*
}
I hope you can help me fixing that.
In any case, thanks for reading.
using UnityEngine;
using System.Collections.Generic;
public class selectObject : MonoBehaviour {
// variables of the regions
public float regionX;
public float regionY;
public float regionWidth;
public float regionHeight;
public static Rect groupRegion;
public static Rect objectRegion;
// variables for detection
private RaycastHit hit;
public List<GameObject> hits = new List<GameObject>();
private Vector3 pos;
public string[] objectName;
private bool hasHit = false;
private Vector2 scrollPosition;
public bool[] show;
void Start() {
// position and size of the regions
regionX = Screen.width / 32;
regionY = Screen.height / 16;
regionWidth = 150;
regionHeight = 200;
groupRegion = new Rect(
regionX,
regionY,
regionWidth,
regionHeight
);
objectRegion = new Rect(
regionX + regionWidth,
regionY,
regionWidth,
regionHeight
);
}
void OnGUI () {
// nothing on right click : nothing appears
if (hasHit) {
GUI.BeginGroup (groupRegion);
// scrolling if too many objects
scrollPosition = GUILayout.BeginScrollView (
scrollPosition,
GUILayout.Width (regionWidth),
GUILayout.Height (regionHeight)
);
// size of the scrollbar
GUI.skin.verticalScrollbar.fixedWidth = 20;
GUI.skin.verticalScrollbarThumb.fixedWidth = 20;
// planar coordinates
GUI.skin.box.fontSize = 20;
GUILayout.Box ("(" + pos.x + "," + pos.y + ")");
// list of names
for (int i = 0; i < hits.Count; i++) {
objectName _= hits *.transform.name;*_
_ if (GUILayout.Button (objectName )) {
show = !show ;
* }
if (show ) {
GUILayout.BeginVertical (“box”);
GUILayout.Box (objectName );
GUILayout.EndVertical ();
Debug.Log (objectName );
}
}
GUILayout.EndScrollView ();
GUI.EndGroup ();
}
}*_
* void Update () {*
* // right click for detection*
* if (Input.GetMouseButtonDown(1)) {*
* Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);*
* // memorize objects if hit*
* if (Physics.Raycast (ray, out hit, 100)) {*
* hasHit = true;*
* pos = hit.transform.position ;*
* hits.Add(hit.transform.gameObject);*
* show = new bool[hits.Count];*
* objectName = new string[hits.Count];*
* } else {*
* hasHit = false;*
* }*
* }*
* }*
}
Hi, thanks for your answer !
It was not what I had in mind but it was instructive. I think I didn’t explain correctly.
I want two regions : a “group region” and at its right side an “object region”.
The group region contains the list [1] of the name of the objects, and if a name is clicked the object region appears to show details about the selected object (for the moment, only the name, that was maybe confusing, sorry).
I realized that the object region must not be defined inside the group region, and your idea of the array “show” is clever. For the moment I obtained the following code, but still without success :
using UnityEngine;
using System.Collections;
public class selectObject : MonoBehaviour {
// variables of the regions
public float regionX;
public float regionY;
public float regionWidth;
public float regionHeight;
public static Rect groupRegion;
public static Rect objectRegion;
// variables for detection
private RaycastHit hit;
private RaycastHit[] hits;
private Vector3 pos;
private string objectName;
private bool hasHit = false;
private Vector2 scrollPosition;
public bool[] show;
void Start() {
// position and size of the regions
regionX = Screen.width / 32;
regionY = Screen.height / 16;
regionWidth = 150;
regionHeight = 200;
groupRegion = new Rect(
regionX,
regionY,
regionWidth,
regionHeight
);
objectRegion = new Rect(
regionX + regionWidth,
regionY,
regionWidth,
regionHeight
);
}
void OnGUI () {
// nothing on right click : nothing appears
if(hasHit) {
GUI.BeginGroup(groupRegion);
// scrolling if too many objects
scrollPosition = GUILayout.BeginScrollView(
scrollPosition,
GUILayout.Width (regionWidth),
GUILayout.Height (regionHeight)
);
// size of the scrollbar
GUI.skin.verticalScrollbar.fixedWidth = 20;
GUI.skin.verticalScrollbarThumb.fixedWidth = 20;
// planar coordinates
GUI.skin.box.fontSize = 20;
GUILayout.Box ("("+pos.x+","+pos.y+")");
// list of names
for (int i = 0; i < hits.Length; i++) {
hit = hits *;*
show = !show*;*
* for (int j = 0; j < hits.Length; j++) {*
* if (j != i) {*
* show [j] = false;*
* }*
* }*
* }*
* }*
* GUILayout.EndScrollView ();*
* GUI.EndGroup();*
* // details about the selected object*
* GUI.BeginGroup(objectRegion);*
* GUILayout.Box (“Box”);*
* for (int i = 0; i < hits.Length; i++) {*
_ hit = hits ;
* objectName = hit.transform.name;
if (show ) {
GUILayout.BeginVertical (“infos”);
GUILayout.Box (objectName);
GUILayout.EndVertical ();
}
}
GUI.EndGroup();
}
}*_
* void Update () {*
* // right click for detection*
* if (Input.GetMouseButtonDown(1)) {*
* Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);*
* // memorize objects if hit*
* if (Physics.Raycast (ray, out hit, 100)) {*
* hasHit = true;*
* pos = hit.transform.position ;*
* hits = Physics.RaycastAll(ray, 100);*
* show = new bool[hits.Length];*
* } else {*
* hasHit = false;*
* }*
* }*
* }*
}
[1] In your code you never empty your list, so new objets are added at each click. I just want the list of the objets detected by raycastAll, nothing more.