Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by jriddensdale · Nov 12, 2015 at 11:17 PM · minecraftstrings

Help with Strings!

So I'm making some kind of Minecraft command block. I really need help with the strings. Heres my code:

var stringToEdit : String = "/type in here"; var clicked : boolean = false; var killCommand = "/kill"; var testCommand = "/test"; var btnTexture : Texture;

         function Update()
         {
         if(killCommand == "/kill")
              {
                     print("Profanity Found");
                }
            }
         //function Update(){
         //CheckCode
              //if(killCommand == "/kill" || killCommand == "/Kill")
              //{
                  //Debug.Log("Killed the player");
                  //Application.LoadLevel(Application.loadedLevel);    
                  
                  
                  
     //}
 //}



 function OnMouseDown(){
         if(Input.GetMouseButtonDown (0))
         {
             clicked = true;
         }
     }
     
 function OnGUI () {
     if(clicked == true)
     {
     // Make a text field that modifies stringToEdit.
     stringToEdit = GUI.TextField (Rect (0, 0, 200, 20), stringToEdit, 35);

 
 
     }
 }

 

But for some reason I can't get this to work. The part I need help with is: function Update() { if(killCommand == "/kill") { print("Profanity Found"); } } I'm going to have multible commands like this. Please Help!!

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
1
Best Answer

Answer by ZefanS · Nov 13, 2015 at 02:05 AM

The main problem in your script seems to be that you never assign the text from the text field to a variable that gets checked for its content. You need to get what the player has typed in and then check if it was a command that your game recognizes.

Here is a modified version that should do approximately what you want.

 #pragma strict
 
 //The string that is edited in the text field
 public var stringToEdit : String;
 //The command as entered by the player
 public var command : String;
 
 //Boolean to track when the player clicks to enable the text field
 public var clicked : boolean;
 //Boolean to track when the player has confirmed the command to enter
 public var shouldCheckCommand : boolean;
 
 public var btnTexture : Texture;
 
 function Start()
 {
     //Initialize variables
     stringToEdit = "/type in here";
     clicked = false;
     shouldCheckCommand = false;
 }
 
 function Update()
 {
     //Check for the mouse click - the text field is invisible
     if(Input.GetMouseButtonDown (0) && clicked == false)
     {
         //Reset the string to default
         stringToEdit = "/type in here";
         clicked = true;
     }
     //Check for the Return/Enter key - the text field is visible
     else if (Input.GetKeyDown(KeyCode.Return) && clicked == true)
     {
         command = stringToEdit; //Here is the key part - the string from the text field is assigned to the command variable
         clicked = false;
         shouldCheckCommand = true;
     }
     
     //Check if the player has confirmed the command entered
     if (shouldCheckCommand == true)
     {
         CheckCommand();
     }
 }
 
 function CheckCommand()
 {
     //Check which command was entered and take the appropriate action
     if (command == "/kill" || command == "/Kill")
     {
         Debug.Log("Kill the player");
     }
     
     shouldCheckCommand = false;
 }
      
 function OnGUI()
 {
     if(clicked == true)
     {
         stringToEdit = GUI.TextField (Rect (0, 0, 200, 20), stringToEdit, 35);
     }
 }

This script activates the text field on a mouse click. Once the command is entered, clicking outside the text field to deselect it and hitting the Return key will "enter" the command.

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 jriddensdale · Nov 14, 2015 at 12:01 AM 0
Share

I'm going to try and use this (:

avatar image jriddensdale · Nov 14, 2015 at 12:07 AM 0
Share

Thank you SO much!!! this was very useful!!

avatar image ZefanS · Nov 16, 2015 at 12:30 AM 0
Share

You're welcome.

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

34 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 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 avatar image avatar image

Related Questions

Load a certain level depending on where objects are placed. 1 Answer

Any way to update a string array mid game?,Update the strings in a string array mid game? 1 Answer

C# Comparing a String to an Array? 1 Answer

Network Sync 0 Answers

Problems adding text to UI Text Component 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