Hi guys!
I am a scripting noob and need some help!
I am building an educational and interactive room. The idea is that when you look at certain objects they highlight and tell the user he can click and get more information (the information displayed in a new GUI window). Now this part i got working. I currently have a Raycast and when i look at objects they change color and tell the user he can click mouse for more information. Now i need that when i click the mouse a GUI window opens up, in this window i want to show pictures with some text, but they are just JPG’s, so i just needs to be a texture i guess 
This is what i have working so far…
using UnityEngine;
using System.Collections;
public class InteractieChild : MonoBehaviour {
public GUIText target;
private bool selected = false;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
renderer.material.color = Color.black;
selected = false;
}
public void OnLookEnter(){
renderer.material.color = Color.red;
target.text = "Click mouse for more information!";
selected = true;
}
void OnGUI(){
Event e = Event.current; //this should be mouseclick spacebar...
if(e.isKey e.character == 'e' selected){ // this should be mouseclick spacebar...
//here the code is needed to open up a GUI window, which can hold a texture);
}
}
}
Thanks so much in advance!
I also tried this…
I have an raycast script attachted to they player that refers to this script
using UnityEngine;
using System.Collections;
public class BasicInteraction2: MonoBehaviour {
public RaycastHit hit;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
Ray ray = Camera.main.ScreenPointToRay(new Vector3(Screen.width/2, Screen.height/2, 0));
if(Physics.Raycast(ray, out hit, 10)){
if(hit.collider.gameObject.GetComponent<InteractieChild2>() !=null){
hit.collider.gameObject.GetComponent<InteractieChild2>().OnLookEnter();
}
}
}
}
this is the referred script called InteractionChild2
using UnityEngine;
using System.Collections;
public class InteractieChild2 : MonoBehaviour {
public GUIText target;
private bool selected = false;
public Texture aTexture;
private bool toggleTxt = false;
private bool toggleImg = false;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
renderer.material.color = Color.black;
selected = false;
}
public void OnLookEnter(){
renderer.material.color = Color.red;
target.text = "druk op E!!";
selected = true;
}
void OnGUI(){
Event e = Event.current;
if(e.isKey e.character == 'e' selected){ if (!aTexture) {
toggleTxt = GUI.Toggle(new Rect(10, 10, 100, 30), toggleTxt, "A Toggle text");
toggleImg = GUI.Toggle(new Rect(10, 50, 50, 50), toggleImg, aTexture);
}}
}
}
Hmmm…
I would do this a little differently. Each object that you want to have “lookable” I would instead, give it an OnMouseOver event. Set a variable that connects it to the OnGUI event. Then clear that variable in an OnMouseExit event.
So…
private bool DrawGUI=false;
void OnMouseOver () {
DrawGUI=true;
}
void OnMouseExit () {
DrawGUI=false;
}
void OnGUI () {
if(DrawGUI){
if (GUI.Button (Rect (10,10,150,100), "I am a button"))
print ("You clicked the button!");
}
}
I was going to suggest something similar, but from his description I wasn’t sure if he was implying that his game was a screen-based interactive room game where you actually have a mouse cursor to hover over objects, or an FPS controller of sorts where you have no cursor and actually just point the camera at objects (in which case it’d be similar except he’d need to raycast from the camera to toggle DrawGUI, instead of OnMouseXXX events).
I have a room in which the user walks around using an FPS controller. I am going to try the drawGUI class, thanks!
Well I am still at a los. I dont get this scripting thing yet. Could some please take a look at my code and add a some scripting so a button popsup! Thanks!
using UnityEngine;
using System.Collections;
public class InteractieChild2 : MonoBehaviour {
public GUIText target;
private bool selected = false;
void Update () {
renderer.material.color = Color.black;
selected = false;
}
public void OnLookEnter(){
renderer.material.color = Color.red;
target.text = "Click mouse";
selected = true;
}
void OnGUI(){
Event e = Event.current;
if(e.isKey e.character == 'e' selected){
[B]// here it should draw a button;[/B]
}
}
}
Try this:
using UnityEngine;
using System.Collections;
public class InteractieChild2 : MonoBehaviour {
public GUIText target;
private bool selected = false;
private bool showButton = false;
void Update () {
renderer.material.color = Color.black;
selected = false;
}
public void OnLookEnter(){
renderer.material.color = Color.red;
target.text = "Click mouse";
selected = true;
}
void OnGUI(){
Event e = Event.current;
if(e.isKey e.character == 'e' selected){
showButton = true; // show the button!
}
if(showButton){ // should we show the button?
if( GUILayout.Button("this is my button") ){ //has the button been clicked?
//button clicked!
showButton = false; // now hide the button after it's been clicked!
}
}
}
}
Thanks so much! That was what I needed!
Now i have a new problem (sorry guys )
Currently the button openup when the player presses e but i want this to be when the primary mouse button is clicked.
I looked it up but can’t get it to work properly because the button may only open when the object is selected (with raycast). But I get errors everytime …
I think the code should look something like this…
Event muisklik = Event.current;
if(muisklik.isMouse muisklik.character == 'mouse1' selected){
showButton = true;