- Home /
How to get similar scripts working on different objects?
I am trying to create a graveyard. In this graveyard you can go up to a stone, hit "E" and it will bring up a short statement in a GUI box and then you can hit exit and move onto the next one.
Anytime I try to copy the file down below and place it on a new stone so I can change the text inside to match a different tombstone, it does not work. The "Press "E" to interact" does not come up, pressing "e" does nothing. It works on the original but creating a new file does not do anything.
I've also tried doing it by object tag which also doesn't work. It only worked on one object and no others.
Let me state the question again as clear as possible: How can I make it so the same thing happens on every stone (show the GUI text and an exit button) while also being able to have different GUI text in the GUI Box that is created?
using UnityEngine;
using System.Collections;
public class InteractA : MonoBehaviour {
public GUIText target;
private bool selected = false;
private GameObject ply;
private GameObject cam;
private bool menu = false;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
selected = false;
target.text = "";
}
public void OnLookEnter(){
target.text = "Press E to interact";
selected = true;
}
void OnGUI() {
ply = GameObject.Find ("First Person Controller");
cam = GameObject.Find ("Main Camera");
Event a = Event.current;
if(a.isKey && a.character == 'e' && selected){
target.enabled = false;
ply.GetComponent<MouseLook>().enabled = false;
ply.GetComponent<CharacterMotor>().enabled = false;
ply.GetComponent<FPSInputController>().enabled = false;
cam.GetComponent<MouseLook>().enabled = false;
Screen.showCursor = true;
menu = true;
}
if(menu){
GUI.Box (new Rect(0, Screen.height/2+125, Screen.width, Screen.height/2), "Together\nHarold John\n Washburn\nFeb. 27, 1940\nJan. 2, 2005");
if(GUI.Button(new Rect(95, Screen.height/2+235, 800, 30), "Exit"))
{
target.enabled = true;
ply.GetComponent<MouseLook>().enabled = true;
ply.GetComponent<CharacterMotor>().enabled = true;
ply.GetComponent<FPSInputController>().enabled = true;
cam.GetComponent<MouseLook>().enabled = true;
Screen.showCursor = false;
menu = false;
target.enabled = true;
}
}
}
}
Answer by Jamora · Aug 25, 2013 at 09:29 PM
You need to call OnLookEnter on each instance of the script when your player is close enough to the stone. You need to call this method every frame after the Update, a good contender is LateUpdate. Calling the method sets your selected true, which in turn causes pressing 'e' to function.
That seems rather complicated to me, I would just rename OnLookEnter to OnTriggerEnter, then put that code in Update into OnTriggerExit
I would also use Input.GetKey(KeyCode.E) instead of that event hassle you currently have in OnGUI. But you know: "If it ain't broken, don't fix it."
Can you show me an example of how I would script that? I'm not exactly sure what that would entail. I tried switching things but nothing works and the first part of the answer about LateUpdate I'm also not sure how I'd do either.
You'll need a collider with the isTrigger checked.
void OnTriggerExit () {
selected = false;
target.text = "";
}
void OnTriggerEnter(){
target.text = "Press E to interact";
selected = true;
}
But re-reading your question after a night's sleep it occurred to me that you probably have another script attached that will call your OnLookEnter. So I suggest you select the working stone, then press ctrl-d (or right click and select duplicate) to duplicate it and see if the clones work as well.
Also, there is no need to create a new script just to change a string. You can just create a public variable and change that per script:
public string inscription = "Together\nHarold John\n Washburn\nFeb. 27, 1940\nJan. 2, 2005";
// in you OnGUI
GUI.Box (new Rect(0, Screen.height/2+125, Screen.width, Screen.height/2), inscription);
I found out my issue, but your the public variable of inscription fixed my real problem. Thanks.
Your answer
Follow this Question
Related Questions
Setting Scroll View Width GUILayout 1 Answer
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
How to access a non static variable by another static variable? 1 Answer
Access custom style from skin via string 1 Answer
how do i have a crosshair turn red when over an enemy? 1 Answer