Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by igfi · Aug 25, 2013 at 09:10 PM · guiscriptingbasics

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;
 }
     }
 }
             }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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."

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image igfi · Aug 26, 2013 at 12:20 AM 0
Share

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.

avatar image Jamora · Aug 26, 2013 at 06:57 AM 0
Share

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);
avatar image igfi · Aug 26, 2013 at 08:31 PM 0
Share

I found out my issue, but your the public variable of inscription fixed my real problem. Thanks.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

16 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges