Referencing intergers in a string

Right I can’t acess the code at the moment so you’ll have to bare with me, I have a locked door that is opened using a key pad which I have created using a a set of ifs and strings, the four numbers I enter into this string is what the code becomes to unlock it. The code is found by finding the 4 numbers painted on the walls through out the building, I have generated a code for each location to create a random number, and created static variables A, B, C and D, all four are randomly generated and corespond to what is on the ways and can be accessed by the actual door lock script fine, I now need to find a way to have the string that I normally just type a 4 digit number for the code to represent these as the four numbers example “1234” which I just can’t do, it basically needs to be :

Var secretCode : String “ABCD”;

But that won’t work obviously

sorry for the vagueness but hope you understand, I’ll try and post the code soon, thanks

In C# it’s pretty easy.

String codeString=A+B+C+D; //This will join the 4 code strings into one string
int codeInt=Convert.ToInt32(codeString); // As long as A,B, C and D are strings made of numerals 
//this will give you the 4 digit integer.

To go the other way…

// This will not work if the code starts with a zero.
int codeInt = 9876;
String codeString=codeInt.ToString(); 
String A=codeString.SubString(0,1);
String B=codeString.SubString(1,1);
String C=codeString.SubString(2,1);
String D=codeString.SubString(3,1);

There are better ways to do all of this, but this is about the simplest.

Have a full (Debug.Log Heavy) sample.

using UnityEngine;
using System.Collections;
using System;

public class TEST1 : MonoBehaviour {

	void OnGUI()
	{
		if (GUI.Button(new Rect(100,100, 100, 100), "DO IT!")){
			DoIt();
		}		
		if (GUI.Button(new Rect(300,300, 100, 100), "DO IT The OTHER Way!")){
			DoIt2();
		}
	}

	void DoIt()
	{
		// This will not work if the code starts with a zero.
		int codeInt = 9876;
		string codeString=codeInt.ToString(); 
		string A=codeString.Substring(0,1);
		string B=codeString.Substring(1,1);
		string C=codeString.Substring(2,1);
		string D=codeString.Substring(3,1);

		Debug.Log("Start with integer 9876");

		Debug.Log ("A:" + A);
		Debug.Log ("B:" + B);
		Debug.Log ("C:" + C);
		Debug.Log ("D:" + D);
	}

	void DoIt2()
	{
		string A = "1";
		string B = "2";
		string C = "3";
		string D = "4";

		Debug.Log ("A:" + A);
		Debug.Log ("B:" + B);
		Debug.Log ("C:" + C);
		Debug.Log ("D:" + D);


		string codeString=A+B+C+D; //This will join the 4 code strings into one string
		int codeInt=Convert.ToInt32(codeString); // As long as A,B, C and D are strings made of numerals 
		//this will give you the 4 digit integer.

		Debug.Log ("Ended with Integer value :"+codeInt.ToString ());
	}
}

Gotcha! So then I think I’m way over complicating this issue and my new suggestion would be to do what you’re almost doing already. Take all randomly generated codes from ABCD and store them inside a single string. Again I’m not particularly familiar with JS but you could try this:

var strSecretCode = "" + RandomiseANumbers.A + RandomiseANumbers.B + 
                         RandomiseANumbers.C + RandomiseANumbers.D;

Then when the player has made 4 entries, just check to see if it equals your randomly generated secret code:

if( strPlayerEntry.Length == 4 )
{
   if( strPlayerEntry == strSecretCode )
   {
       print("Entered the correct code!");
       door1.rigidbody.isKinematic = false;
       door2.rigidbody.isKinematic = false;
   }
}

I hope this one finally helps.

heres my code, i think i get what you guys are saying I’m just confused as to how to implement it:

var A2 = RandomiseANumbers.A;
var B2 = RandomiseBNumbers.B;
var C2 = RandomiseCNumbers.C;
var D2 = RandomiseDNumbers.D;

var code;

var door1:GameObject;
var door2:GameObject;
var locked : AudioClip;
var unlocked : AudioClip;

private var strSecretCode : String = ""+(RandomiseANumbers.A);
var strPlayerEntry : String = "";

private var buttonPressed : boolean = false;

var codeEntered : String = "";

function Start () {




print(strSecretCode);



}

function OnTriggerEnter( hit : Collider )
{
 	if(hit.gameObject.tag=="Code")
 	{
		buttonPressed = true;	
   	}
	if(hit.gameObject.tag=="Code")
 	{
		codeEntered = codeEntered + "C";	
   	}  
   	
}

function OnTriggerExit( hit : Collider )
{
 	if(hit.gameObject.tag=="Code")
 	{
		buttonPressed = false;
		codeEntered="";
		strPlayerEntry="";
		
		codeCount = 0;
   	}  	
}  
      
            
   function OnGUI()
   {
   if (codeEntered=="C")
   {
   	if( buttonPressed )
   	{
 GUI.Box (Rect (Screen.width/2-85,Screen.height/2-40,200,130), "Enter the security code");
			
			GUI.Button(Rect (Screen.width/2-15,Screen.height/2+60,55,20),strPlayerEntry);
			
			if(GUI.Button(Rect (Screen.width/2-25,Screen.height/2-20,25,20), "1")){
				strPlayerEntry += "1";
			}

			else
			if(GUI.Button(Rect (Screen.width/2,Screen.height/2-20,25,20), "2"))
			{
				strPlayerEntry += "2";
			}
			else
			if(GUI.Button(Rect (Screen.width/2+25,Screen.height/2-20,25,20), "3")){
				strPlayerEntry += "3";
			}				
			else
			if(GUI.Button(Rect (Screen.width/2-25,Screen.height/2,25,20), "4")){
				strPlayerEntry += "4";
			}			
			else
			if(GUI.Button(Rect (Screen.width/2,Screen.height/2,25,20), "5")){
				strPlayerEntry += "5";
			}			
			else
			if(GUI.Button(Rect (Screen.width/2+25,Screen.height/2,25,20), "6")){
				strPlayerEntry += "6";
			}
			else
			if(GUI.Button(Rect (Screen.width/2-25,Screen.height/2+20,25,20), "7"))
			{
				strPlayerEntry += "7";
			}
						else
			if(GUI.Button(Rect (Screen.width/2,Screen.height/2+20,25,20), "8"))
			{
				strPlayerEntry += "8";
			}
			else
			if(GUI.Button(Rect (Screen.width/2+25,Screen.height/2+20,25,20), "9"))
			{
				strPlayerEntry += "9";
			}
			else
			if(GUI.Button(Rect (Screen.width/2,Screen.height/2+40,25,20), "0"))
			{
				strPlayerEntry += "0";
			}
			
			
			if( strPlayerEntry.Length == 4 )
			{
			if( strPlayerEntry == strSecretCode )
			{
				print("Entered the correct code!");
				door1.rigidbody.isKinematic = false;
				door2.rigidbody.isKinematic = false;
			}
			else
			{ 
			strPlayerEntry = "";
			}
			}
			
			}
			}
			


																								}