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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by Native · Oct 01, 2013 at 03:50 AM · guivariable

How to Update Variable from GUI

I'm using the GUI to make a Turn Based Combat System with buttons. I'm having problems getting the 'void OnGUI' to update a variable outside of the GUI and keep it when a button is clicked. My goal is to have the object move to the enemy and move back to position. Here is a stripped down version of my code. I'm still new to coding any help will be appreciate.

 using UnityEngine;
 using System.Collections;
  
 public class Menu : MonoBehaviour
 {
  
     private string[] selStrings = { "Attacks", "Magic" };
     enemyObject EObject = new enemyObject();
  
     void OnGUI()
     {
        if(player.hitPoints > 0 && enemy.hitPoints > 0)
        {
           selGridInt = GUI.SelectionGrid(new Rect(0, Screen.height - 55, 160, 47), selGridInt, selStrings, 2);
           if(turn == 1)
              if(selGridInt == 0)
                 if (GUI.Button (new Rect (255, Screen.height - 48, 100, 40), "Punch"))
                 {
                     punch(player, enemy);
                     EObject.wantingToMove(2);
                     turn -= 1;
                 }
         }
     }
 }
  
 public class enemyObject : MonoBehaviour
 {
  
     public int run;
  
     void Update ()
     {
        if(run == 2)
           transform.Translate(new Vector3(0,0,1) * Time.deltaTime);
     }
  
     public int wantingToMove(int a)
     {
         run = a;
         return run;
     }

 }
Comment
Add comment · Show 2
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 robertbu · Oct 01, 2013 at 03:53 AM 0
Share

I formatted your code for you using the 101/010 button, but your incorrect indenting and extra lines still makes this code difficult to read. Cleaning it up will get you a better response.

avatar image srancsik · Oct 03, 2013 at 03:33 PM 0
Share

Did you try to make the enemyObject public? I think the problem is that it is available in the OnGUI, but void Update() can not see it.

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by robertbu · Oct 03, 2013 at 03:40 PM

'enemyObject' is derived from MonoBehaviour, which means that this script should be attached to a game object and exist as a 'component' of that game object. With these kinds of classes, you don't use 'new' to create them as you've done on line 8. You need to first find the game object this script is attached to, then you need to find the component.

http://docs-jp.unity3d.com/Documentation/ScriptReference/index.Accessing_Other_Game_Objects.html

http://docs-jp.unity3d.com/Documentation/ScriptReference/index.Accessing_Other_Components.html

Comment
Add comment · 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

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

How can I Include string variables in Rich Text? 1 Answer

variable before (dot) 2 Answers

Optimizing OnGUI 1 Answer

Limiting digits in GUI displayed variables (or "snap to" GUI slider). 1 Answer

Input player name to a variable 2 Answers


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