Cheat code (C#) adding additional lives / infinite lives

Hi everyone i was wondering if anyone would be able to assist me on a issue i am having regarding adding a cheat code to a game. The game i am currently developing is a arckanoid/bricker style clone, i am using this to teach myself C# and this is my first game creation in unity and i am a little suck.

Game: Arckanoid/Bricker
Problem: I would like to add additional/infinite lives on a specific key press (Say { “c”, “h”, “e”, “a”, “t” }; for example

Current Paddle script below I am assuming its possible to add to the main paddle script, would greatly appericate any advice on how to add a “cheat code” to the below script

Many thanks, and happy holidays

using UnityEngine;
using System.Collections;

public class PaddleScript : MonoBehaviour {
	
	float paddleSpeed = 10f;
	public GameObject ballPrefab;
	GameObject attachedBall = null;
	
	int lives = 5;
	GUIText guiLives;
	
	int score = 0;
	
	public GUISkin scoreboardSkin;
	
	
	// Use this for initialization
	void Start () {
		DontDestroyOnLoad(gameObject);
		DontDestroyOnLoad(GameObject.Find("guiLives"));
		
		guiLives = GameObject.Find("guiLives").GetComponent<GUIText>();
		guiLives.text = "Lives: " + lives;
		
		SpawnBall();
	}
	
	public void OnLevelWasLoaded( int level ) {
		SpawnBall();
	}
	
	public void AddPoint(int v) {
		score += v;
	}
	
	public void LoseLife() {
		lives--;
		guiLives.text = "Lives: " + lives;
		if ( lives > 0 )
			SpawnBall();
		else {
			Destroy(gameObject);
			Application.LoadLevel("gameOver");
		}
	}
	
	public void SpawnBall() {
		// Spawn/Instantiate new ball
		if( ballPrefab == null ) {
			Debug.Log (" link to the ball prefab in the inspector!");
			return;
		}
		
		attachedBall = (GameObject)Instantiate( ballPrefab, transform.position + new Vector3(0, .75f, 0), Quaternion.identity );
		
		
	}
	
	void OnGUI() {
		GUI.skin = scoreboardSkin;
		GUI.Label( new Rect(0,10,300,100), "Score: " + score);
	}
	
	// Update is called once per frame
	void Update () {
		// Left-Right
		transform.Translate( paddleSpeed * Time.deltaTime * Input.GetAxis( "Horizontal" ), 0, 0 );
		
		if ( transform.position.x > 7.4f ) {
			transform.position = new Vector3( 7.4f, transform.position.y, transform.position.z );
		}
		if ( transform.position.x < -7.4f ) {
			transform.position = new Vector3( -7.4f, transform.position.y, transform.position.z );
		}
		
		if( attachedBall ) {
			Rigidbody ballRigidbody = attachedBall.rigidbody;
			ballRigidbody.position = transform.position + new Vector3(0, .75f, 0);
			
			if( Input.GetButtonDown( "LaunchBall" ) ) {
				// Fire the ball
				ballRigidbody.isKinematic = false;
				ballRigidbody.AddForce(300f * Input.GetAxis( "Horizontal" ), 300f, 0);
				attachedBall = null;
			}
		}
	}
	
	void FixedUpdate() {
	}
	
	void LateUpdate() {
	}
	
	void OnCollisionEnter( Collision col ) {
		foreach (ContactPoint contact in col.contacts) {
			if( contact.thisCollider == collider ) {
				// This is the paddle's contact point
				float english = contact.point.x - transform.position.x;
				
				contact.otherCollider.rigidbody.AddForce( 300f * english, 0, 0);
			}
		}
	}
}

Here’s a little snippet which should get you on the right track, the concept is pretty simple - Increment some progress counter on each keypress, while a timer resets the counters progress if you don’t press them fast enough. Sorry for the crappy formatting, I’m too lazy to make it any better but at least it’s readable.

int cheat_progress = 0;
float cheat_delay = 0.0f;

if(cheat_progress == 0  Input.GetKeyDown('c')){
    ++cheat_progress; cheat_delay = 1.0f;
    } else if(cheat_progress == 1  Input.GetKeyDown('h')){
        ++cheat_progress; cheat_delay = 1.0f;
    } else if(cheat_progress == 2  Input.GetKeyDown('e')){
        ++cheat_progress; cheat_delay = 1.0f;
    } else if(cheat_progress == 3  Input.GetKeyDown('a')){
        ++cheat_progress; cheat_delay = 1.0f;
    } else if(cheat_progress == 4  Input.GetKeyDown('t')){
        cheat_progress = 0;
        //Some cheats here
    }
if(cheat_delay > 0.0f){
    cheat_delay -= Time.deltaTime;
    if(cheat_delay <= 0.0f){
        cheat_delay = 0.0f;
        cheat_progress = 0;
    }
}

Thank you very much for taking the time to write that, i don’t know if i am just very tired but i am struggling with implementing this and getting it to work.

Thanks again for your time, its greatly appreciated, hopefully my limited C skills will get this working tomrrow

No problem! I’m sure you can figure it out

not sure what i am doing wrong josh707, but i cant get the script to work. should this be implemented as a separate script or incorporated in to the above code i stated … sorry but i cant figure this out. My C# is currently limited a

Hmm, it should be in the same script if you want it to affect lives without using GetComponent, in the Update function. This should work just putting it on an empty gameobject or under your stuff in Update but I haven’t tested it:

private int cheat_progress = 0;
private float cheat_delay = 0.0f; 

void Update(){
    if(cheat_progress == 0  Input.GetKeyDown('c')){
        ++cheat_progress; 
        cheat_delay = 1.0f;
    } 
    else if(cheat_progress == 1  Input.GetKeyDown('h')){
        ++cheat_progress; 
        cheat_delay = 1.0f;
    } 
    else if(cheat_progress == 2  Input.GetKeyDown('e')){
        ++cheat_progress; 
        cheat_delay = 1.0f;
    } 
    else if(cheat_progress == 3  Input.GetKeyDown('a')){
        ++cheat_progress; 
        cheat_delay = 1.0f;
    } 
    else if(cheat_progress == 4  Input.GetKeyDown('t')){
        cheat_progress = 0; 
        Debug.Log("Enabled");
        //Or in your case lives += 1 or something
    }
    cheat_delay = Mathf.Max(cheat_delay - Time.deltaTime, 0.0f);
    if(cheat_delay == 0.0f){
        cheat_progress = 0;
    }
}

I made the formatting a little better so maybe that will help

Josh707 thank you for all the help so far, i have been trying to incorporate the script you wrote in to my “paddle” script but i just cant seam to get it to work.

Would appericate any help making the bottom cheat script work within the bellow script, i have been struggling for a week now and i am no closer to making it work

PLS Help

My original paddle script is

using UnityEngine;
using System.Collections;

public class PaddleScript : MonoBehaviour {
	
	float paddleSpeed = 10f;
	public GameObject ballPrefab;
	GameObject attachedBall = null;
	
	public int lives;
	GUIText guiLives;
	
	int score = 0;
	
	public GUISkin scoreboardSkin;
	
	
	// Use this for initialization
	void Start () {
		DontDestroyOnLoad(gameObject);
		DontDestroyOnLoad(GameObject.Find("guiLives"));
		
		guiLives = GameObject.Find("guiLives").GetComponent<GUIText>();
		guiLives.text = "Lives: " + lives;
		
		SpawnBall();
	}
	
	public void OnLevelWasLoaded( int level ) {
		SpawnBall();
	}
	
	public void AddPoint(int v) {
		score += v;
	}
	
	public void LoseLife() {
		lives--;
		guiLives.text = "Lives: " + lives;
		if ( lives > 0 )
			SpawnBall();
		else {
			Destroy(gameObject);
			Application.LoadLevel("gameOver");
		}
	}
	
	public void SpawnBall() {
		// Spawn/Instantiate new ball
		if( ballPrefab == null ) {
			Debug.Log ("Hey, dummy, you forgot to link to the ball prefab in the inspector!");
			return;
		}
		
		attachedBall = (GameObject)Instantiate( ballPrefab, transform.position + new Vector3(0, .75f, 0), Quaternion.identity );
		
		
	}
	
	void OnGUI() {
		GUI.skin = scoreboardSkin;
		GUI.Label( new Rect(0,10,300,100), "Score: " + score);
	}
	
	// Update is called once per frame
	void Update () {
		// Left-Right
		transform.Translate( paddleSpeed * Time.deltaTime * Input.GetAxis( "Horizontal" ), 0, 0 );
		
		if ( transform.position.x > 7.4f ) {
			transform.position = new Vector3( 7.4f, transform.position.y, transform.position.z );
		}
		if ( transform.position.x < -7.4f ) {
			transform.position = new Vector3( -7.4f, transform.position.y, transform.position.z );
		}
		
		if( attachedBall ) {
			Rigidbody ballRigidbody = attachedBall.rigidbody;
			ballRigidbody.position = transform.position + new Vector3(0, .75f, 0);
			
			if( Input.GetButtonDown( "LaunchBall" ) ) {
				// Fire the ball
				ballRigidbody.isKinematic = false;
				ballRigidbody.AddForce(300f * Input.GetAxis( "Horizontal" ), 300f, 0);
				attachedBall = null;
			}
		}
	}
	
	void FixedUpdate() {
	}
	
	void LateUpdate() {
	}
	
	void OnCollisionEnter( Collision col ) {
		foreach (ContactPoint contact in col.contacts) {
			if( contact.thisCollider == collider ) {
				// This is the paddle's contact point
				float english = contact.point.x - transform.position.x;
				
				contact.otherCollider.rigidbody.AddForce( 300f * english, 0, 0);
			}
		}
	}
}

Cheat code to implement in to the above script

private int cheat_progress = 0;
private float cheat_delay = 0.0f; 

void Update(){

    if(cheat_progress == 0  Input.GetKeyDown('c')){
        ++cheat_progress; 
        cheat_delay = 1.0f;
    } 
    else if(cheat_progress == 1  Input.GetKeyDown('h')){
        ++cheat_progress; 
        cheat_delay = 1.0f;
    } 
    else if(cheat_progress == 2  Input.GetKeyDown('e')){
        ++cheat_progress; 
        cheat_delay = 1.0f;
    } 
    else if(cheat_progress == 3  Input.GetKeyDown('a')){
        ++cheat_progress; 
        cheat_delay = 1.0f;
    } 
    else if(cheat_progress == 4  Input.GetKeyDown('t')){
        cheat_progress = 0; 
        Debug.Log("Enabled");
	lives += 100
        //Or in your case lives += 1 or something
    }
    cheat_delay = Mathf.Max(cheat_delay - Time.deltaTime, 0.0f);
    if(cheat_delay == 0.0f){
        cheat_progress = 0;
    }
}

Hmm, it should be relatively simple - You can write the code into a method in the same script and then just call it in Update. The keys must use KeyCodes rather than a string in C# though.

//Your variables
...
private int cheat_progress = 0;
private float cheat_delay = 0.0f;

void UpdateCheats() {
    if(cheat_progress == 0  Input.GetKeyDown(KeyCode.C)){
        ++cheat_progress;
        cheat_delay = 1.0f;
    }
    else if(cheat_progress == 1  Input.GetKeyDown(KeyCode.H)){
        ++cheat_progress;
        cheat_delay = 1.0f;
    }
    else if(cheat_progress == 2  Input.GetKeyDown(KeyCode.E)){
        ++cheat_progress;
        cheat_delay = 1.0f;
    }
    else if(cheat_progress == 3  Input.GetKeyDown(KeyCode.A)){
        ++cheat_progress;
        cheat_delay = 1.0f;
    }
    else if(cheat_progress == 4  Input.GetKeyDown(KeyCode.T)){
        cheat_progress = 0;
        lives += 100;
    }
    cheat_delay = Mathf.Max(cheat_delay - Time.deltaTime, 0.0f);
    if(cheat_delay == 0.0f){
        cheat_progress = 0;
    }
}

void Update(){
        transform.Translate( paddleSpeed * Time.deltaTime * Input.GetAxis( "Horizontal" ), 0, 0 );
        if ( transform.position.x > 7.4f ) {
            transform.position = new Vector3( 7.4f, transform.position.y, transform.position.z );
        }
        if ( transform.position.x < -7.4f ) {
            transform.position = new Vector3( -7.4f, transform.position.y, transform.position.z );
        }
        if( attachedBall ) {
            Rigidbody ballRigidbody = attachedBall.rigidbody;
            ballRigidbody.position = transform.position + new Vector3(0, .75f, 0);
            if( Input.GetButtonDown( "LaunchBall" ) ) {
                // Fire the ball
                ballRigidbody.isKinematic = false;
                ballRigidbody.AddForce(300f * Input.GetAxis( "Horizontal" ), 300f, 0);
                attachedBall = null;
            }
        }
        UpdateCheats();
    }

I have managed to implement the code in to the main script, now it does not throw out any errors however when entering “CHEAT” the lives stay the same and the ball is deleted.

Really struggling with this one, josh707 you have been extremely helpful with this problem and i cant thank you enough for the help up till now mate.

Can you see any errors with the below code i just cant get it to work, i am pushing way above my c# skill level here with

using UnityEngine;
using System.Collections;

public class PaddleScript : MonoBehaviour {
	
	float paddleSpeed = 10f;
	public GameObject ballPrefab;
	GameObject attachedBall = null;
	
	public int lives;
	GUIText guiLives;
	
	int score = 0;
	
	public GUISkin scoreboardSkin;
	
	
	// Use this for initialization
	void Start () {
		DontDestroyOnLoad(gameObject);
		DontDestroyOnLoad(GameObject.Find("guiLives"));
		
		guiLives = GameObject.Find("guiLives").GetComponent<GUIText>();
		guiLives.text = "Lives: " + lives;
		
		SpawnBall();
	}
	
	public void OnLevelWasLoaded( int level ) {
		SpawnBall();
	}
	
	public void AddPoint(int v) {
		score += v;
	}
	
	public void LoseLife() {
		lives--;
		guiLives.text = "Lives: " + lives;
		if ( lives > 0 )
			SpawnBall();
		else {
			Destroy(gameObject);
			Application.LoadLevel("gameOver");
		}
	}
	
	public void SpawnBall() {
		// Spawn/Instantiate new ball
		if( ballPrefab == null ) {
			Debug.Log ("Hey, dummy, you forgot to link to the ball prefab in the inspector!");
			return;
		}
		
		attachedBall = (GameObject)Instantiate( ballPrefab, transform.position + new Vector3(0, .75f, 0), Quaternion.identity );
		
		
	}
	
	void OnGUI() {
		GUI.skin = scoreboardSkin;
		GUI.Label( new Rect(0,10,300,100), "Score: " + score);
	}
	
	// Update is called once per frame
	private int cheat_progress = 0;
	private float cheat_delay = 0.0f;
	
	void UpdateCheats() {
		
		if(cheat_progress == 0  Input.GetKeyDown(KeyCode.C)){
			++cheat_progress;
			cheat_delay = 1.0f;
		}
		else if(cheat_progress == 1  Input.GetKeyDown(KeyCode.H)){
			++cheat_progress;
			cheat_delay = 1.0f;
		}
		else if(cheat_progress == 2  Input.GetKeyDown(KeyCode.E)){
			++cheat_progress;
			cheat_delay = 1.0f;
		}
		else if(cheat_progress == 3  Input.GetKeyDown(KeyCode.A)){
			++cheat_progress;
			cheat_delay = 1.0f;
		}
		else if(cheat_progress == 4  Input.GetKeyDown(KeyCode.T)){
			cheat_progress = 0;
			lives += 100;
		}
		cheat_delay = Mathf.Max(cheat_delay - Time.deltaTime, 0.0f);
		if(cheat_delay == 0.0f){
			cheat_progress = 0;
			Debug.Log ("Cheater!");
			return;
		}
	}
	
	void Update(){
		transform.Translate( paddleSpeed * Time.deltaTime * Input.GetAxis( "Horizontal" ), 0, 0 );
		if ( transform.position.x > 7.4f ) {
			transform.position = new Vector3( 7.4f, transform.position.y, transform.position.z );
		}
		if ( transform.position.x < -7.4f ) {
			transform.position = new Vector3( -7.4f, transform.position.y, transform.position.z );
		}
		if( attachedBall ) {
			Rigidbody ballRigidbody = attachedBall.rigidbody;
			ballRigidbody.position = transform.position + new Vector3(0, .75f, 0);
			if( Input.GetButtonDown( "LaunchBall" ) ) {
				// Fire the ball
				ballRigidbody.isKinematic = false;
				ballRigidbody.AddForce(300f * Input.GetAxis( "Horizontal" ), 300f, 0);
				attachedBall = null;
			}
		}
		UpdateCheats();
	}
	
	void FixedUpdate() {
	}
	
	void LateUpdate() {
	}
	
	void OnCollisionEnter( Collision col ) {
		foreach (ContactPoint contact in col.contacts) {
			if( contact.thisCollider == collider ) {
				// This is the paddle's contact point
				float english = contact.point.x - transform.position.x;
				
				contact.otherCollider.rigidbody.AddForce( 300f * english, 0, 0);
			}
		}
	}
}

Hehe honestly I don’t understand why that script wouldn’t work, it looks like it should. Perhaps there’s another key to destroy the ball in another script? Also you may need to update the gui text like you have earlier in the script: guiLives.text = "Lives: " + lives;
No problem though, I like to help!

I realise this is an old thread, but I had a similar problem for a similar reason (I imagine) recently. I used the “platform defines” instead: Adding Cheats and Features to a Unity Game for Development Only