Search Unity

UI not updating when calling GetComponent<Text>();

Discussion in 'UGUI & TextMesh Pro' started by Numonic, Aug 21, 2018.

  1. Numonic

    Numonic

    Joined:
    Mar 22, 2009
    Posts:
    241
    Hi everyone I'm having a problem I have a UI with PlayerPrefs and trying out Singletons as well to keep information such as Score, Health and Player lives. They seem to carry over to the next scene because I can see it via the inspector but the UI doesn't update. Does anyone have any ideas? The player is Leonardo from a TMNT Reshelled game I'm working on fan based of course. Also the CharacterStats class has the
    variables public so I can view them in the inspector.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine.SceneManagement;
    4.  
    5. using UnityEngine.UI;
    6. using UnityEngine;
    7.  
    8. public class CharacterStats : MonoBehaviour {
    9.  
    10.  
    11.     static CharacterStats characterStats;
    12.     //public GameManager gameManager;
    13.     [Header("Player1")]
    14.     public GameObject player1;
    15.     public Image playerimage1;
    16.     public Slider leoHealthSlider;
    17.     public Text leoScore;
    18.     public Text leoLife;
    19.     public  int lifeLeo = 3;
    20.     public  int healthLeo = 100;
    21.     public int scoreLeo = 00000;
    22.     public int damageTakenLeo = 2;
    23.     public int snowPlowDamage = 15;
    24.     public int rodneyTenticleDamage = 2;
    25.     public int swordAttackDamage = 50;
    26.     public int kickAttackDamage = 20;
    27.     public int bossShootDamage = 20;
    28.     public int bossKickDamage = 15;
    29.     public int bossTackleDamge = 18;
    30.  
    31.     public GameObject pizza;
    32.     public int pizzaAddHealth = 100;
    33.     BallSpawn ballSpawn;
    34.     public AudioSource pizzaTime;
    35.     public AudioSource itemPickUp;
    36.     public PlayerController playerController;
    37.  
    38.     /*
    39.     [Header("Player2")]
    40.     public GameObject player2;
    41.     public Image playerimage2;
    42.     public Slider mikeHealthSlider;
    43.     public Text mikeScore;
    44.     public Text mikeLife;
    45.     public int healthMike = 100;
    46.     public int scoreMike = 00000;
    47.     public int lifeMike = 3;
    48.  
    49.  
    50.  
    51.  
    52.     [Header("Player3")]
    53.     public GameObject player3;
    54.     public Image playerimage3;
    55.     public Slider donHealthSlider;
    56.     public Text donScore;
    57.     public Text donLife;
    58.     public int healthDon = 100;
    59.     public int scoreDon = 00000;
    60.     public int lifeDon = 3;
    61.  
    62.  
    63.  
    64.  
    65.  
    66.  
    67.     [Header("Player4")]
    68.     public GameObject player4;
    69.     public Image playerimage4;
    70.     public Slider raphHealthSlider;
    71.     public Text raphScore;
    72.     public Text raphLife;
    73.     public int healthRaph = 100;
    74.     public int scoreRaph = 00000;
    75.     public int lifeRaph = 3;
    76.     */
    77.  
    78.  
    79.     void Awake()
    80.     {
    81.         /*
    82.         if (gameManager == null)
    83.         {
    84.             gameManager = GetComponent<GameManager>();
    85.  
    86.         }
    87.         gameManager = GetComponent<GameManager>();
    88.         */
    89.  
    90.         if(GameManager.Instance == null)
    91.         {
    92.             GameManager.Instance = GetComponent<GameManager>();
    93.         }
    94.  
    95.  
    96.         //Grabs life and into and saves it.
    97.         scoreLeo = PlayerPrefs.GetInt("scoreLeo", scoreLeo);
    98.         lifeLeo = PlayerPrefs.GetInt("lifeLeo", lifeLeo);
    99.         healthLeo = PlayerPrefs.GetInt("healthLeo", healthLeo);
    100.         ///////////////////////////////////////////////////////
    101.  
    102.        // PlayerPrefs.DeleteAll();  //use this when things get wonky with score, lives and health
    103.  
    104.  
    105.      
    106.  
    107.  
    108.         if (ballSpawn ==null)
    109.         {
    110.             ballSpawn = GetComponent<BallSpawn>();
    111.         }
    112.  
    113.  
    114.    
    115.         if (playerController ==null)
    116.         {
    117.             playerController = GetComponent<PlayerController>();
    118.  
    119.         }
    120.         /////////////////////
    121.         //// Leo life status
    122.         if (playerimage1 == null)
    123.         {
    124.             playerimage1 = GetComponent<Image>();
    125.  
    126.         }
    127.  
    128.         if (leoHealthSlider == null)
    129.         {
    130.             leoHealthSlider = GetComponent<Slider>();
    131.             leoHealthSlider = GetComponentInChildren<Slider>();
    132.  
    133.         }
    134.  
    135.         if (leoScore == null)
    136.         {
    137.             leoScore = GetComponent<Text>();
    138.             leoScore = GetComponentInChildren<Text>();
    139.         }
    140.  
    141.         if (leoLife == null)
    142.         {
    143.             leoLife = GetComponent<Text>();
    144.             leoLife = GetComponentInChildren<Text>();
    145.  
    146.         }
    147.         ////////////////////////////////
    148.         leoScore.text = "  " + scoreLeo;
    149.         leoLife.text = "x 3";
    150.  
    151.  
    152.         /////
    153.     }
    154.  
    155.  
    156.  
    157.     // Update is called once per frame
    158.     void Update()
    159.     {
    160.  
    161.     }
    162.  
    163.     //Leonardo Health
    164.     public void LeoHealth()
    165.     {
    166.          //Added this to set Health between levels.
    167.          PlayerPrefs.SetInt("healthLeo", healthLeo);
    168.  
    169.         leoHealthSlider.value = healthLeo;
    170.  
    171.         //healthLeo = 100;
    172.    
    173.  
    174.     }
    175.  
    176.     ////////////////////////////
    177.     //Set Life Test/////////////
    178.     public void LeoLives()
    179.     {
    180.  
    181.         PlayerPrefs.GetInt("lifeLeo", lifeLeo);
    182.         leoLife.text = "x " + lifeLeo.ToString();
    183.  
    184.             lifeLeo -= 1;
    185.  
    186.         leoHealthSlider.value = healthLeo;
    187.  
    188.         //turned this on for a bit
    189.            healthLeo = 100; //
    190.           //had it off originally
    191.    
    192.         if (healthLeo == 0 && lifeLeo == 0)
    193.         {
    194.             healthLeo = 0;
    195.             //gameManager.GameOverScreen();
    196.             GameManager.Instance.GameOverScreen();
    197.             print("All 3 lives are gone and so is health we restart game.");
    198.  
    199.  
    200.         }
    201.  
    202.         if (healthLeo <=0)
    203.         {
    204.  
    205.             //healthLeo = 100;
    206.             healthLeo = 0;
    207.  
    208.         }
    209.  
    210.     }
    211.  
    212.     /////////////////////////////////
    213.     //Waiting Some time to restart game smoother transition
    214.     IEnumerator WaitSometimeForRestartRoutine()
    215.     {
    216.         yield return new WaitForSeconds(2f);
    217.         SceneManager.LoadScene(0);
    218.         print("We are restarting Game.");
    219.  
    220.     }
    221.     ///Waiting Some time to restart game smoother transition
    222.     ///
    223.     public void AddScore()
    224.     {
    225.         //Added this to test out score between levels.
    226.         PlayerPrefs.SetInt("scoreLeo", scoreLeo);
    227.  
    228.         leoScore.text = "" + scoreLeo.ToString();
    229.         scoreLeo++; //Add to score
    230.         print("we are adding score for footsolder defeat.");
    231.  
    232.     }
    233.  
    234.  
    235.     public void AddHealth()
    236.     {
    237.         //Added this to test out Health between levels.
    238.  
    239.         PlayerPrefs.SetInt("healthLeo", healthLeo);
    240.  
    241.         // Refill life with 100
    242.         leoHealthSlider.value = healthLeo;
    243.  
    244.         healthLeo += pizzaAddHealth;
    245.  
    246.         if(healthLeo > pizzaAddHealth)
    247.         {
    248.             healthLeo = 100;
    249.         }
    250.  
    251.         pizzaTime.Play();
    252.         itemPickUp.Play();
    253.         //print("Running AddHealth Method.");
    254.         //print(healthLeo);
    255.  
    256.     }
    257.  
    258.  
    259.     public void DamageLeo(int damageTakenLeo)
    260.     {
    261.         PlayerPrefs.SetInt("healthLeo", healthLeo);
    262.         healthLeo -= damageTakenLeo;
    263.         leoHealthSlider.value = healthLeo;
    264.         characterStats.leoLife.text = "x " + characterStats.lifeLeo.ToString();
    265.         Debug.Log("Checking DamageLeo()");
    266.     }
    267. }
    268.  

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityStandardAssets.CrossPlatformInput;
    4. using UnityEngine;
    5.  
    6. public class PlayerController : MonoBehaviour {
    7.  
    8.     public LeonardoAttackTrailScript leoTrailAttack;
    9.     protected PlayerController playerController;
    10.     public CharacterController characterController;
    11.     public CapsuleCollider capsuleCollider;
    12.     public Vector3 respawnPosition;
    13.     public Renderer rend;
    14.     public  Rigidbody rigidBody;
    15.     public AudioSource leokick;
    16.     public AudioSource metalHitSound;
    17.     public AudioSource footPunchSound;
    18.     public AudioSource explosionSound;
    19.     public AudioSource shellShock;
    20.     public AudioSource electricshock;
    21.     public AudioSource death;
    22.     public AudioSource attackSound;
    23.     public GameObject lKatanaAttackBox, rKatanaAttackBox, lFootAttackBox, rFootAttackBox; //player attackboxes
    24.     //public AudioSource leoJump;
    25.  
    26.  
    27.     public ToraDoDamage toraDoDamage;
    28.  
    29.     private TenticleShock tenticleShock;
    30.     private GameObject roadKillRodneyPrefab;
    31.  
    32.     private FootDoDamage footdoDamage;
    33.     public BallSpawn ballSpawn;
    34.     public GameManager gameManager;
    35.     public CharacterStats characterStats;
    36.     public CameraFollow cameraFollow;
    37.  
    38.  
    39.     [Header("Player Settings")]
    40.     public float walkSpeed =2;
    41.     public float runSpeed =6;
    42.     public float gravity = -12f;
    43.     public float jumpHeight = 1f;
    44.     public int attknumber;
    45.     public bool canJump;
    46.     Animator anim;
    47.  
    48.  
    49.     [Range(0,1)]
    50.     public float airControlPercent;
    51.     private CharacterController controller;
    52.  
    53.    
    54.     public float turnSmoothTime = 0.2f;
    55.     float turnSmoothVelocity;
    56.  
    57.     public float speedSmoothTime = 0.1f;
    58.     float speedSmoothVelocity;
    59.     float currentSpeed;
    60.     float velocityY;
    61.  
    62.  
    63.     // Use this for initialization
    64.     void Awake()
    65.     {
    66.  
    67.         if(toraDoDamage == null)
    68.         {
    69.             toraDoDamage = GetComponent<ToraDoDamage>();
    70.         }
    71.  
    72.  
    73.  
    74.         if(characterController ==null)
    75.         {
    76.             characterController = GetComponent<CharacterController>();
    77.        
    78.         }
    79.  
    80.         if(capsuleCollider ==null)
    81.         {
    82.             capsuleCollider = GetComponent<CapsuleCollider>();
    83.         }
    84.  
    85.         if(cameraFollow ==null)
    86.         {
    87.             cameraFollow = GameObject.Find("MainCamera").GetComponent<CameraFollow>();
    88.  
    89.         }
    90.         respawnPosition = transform.position;
    91.    
    92.         if(rend ==null)
    93.         {
    94.             rend = GameObject.Find("Leonardo").GetComponent<Renderer>();//originally "Leonardo
    95.  
    96.         }
    97.  
    98.         if (electricshock ==null)
    99.         {
    100.             electricshock = GameObject.Find("electricshock").GetComponent<AudioSource>();
    101.         }
    102.         if (death == null)
    103.         {
    104.             death = GameObject.Find("death").GetComponent<AudioSource>();
    105.         }
    106.  
    107.         if (metalHitSound == null)
    108.         {
    109.             metalHitSound = explosionSound = GameObject.Find("metalhit").GetComponent<AudioSource>();
    110.  
    111.         }
    112.  
    113.         if (explosionSound == null)
    114.         {
    115.             explosionSound = GameObject.Find("explosion").GetComponent<AudioSource>();
    116.         }
    117.  
    118.  
    119.             if (tenticleShock != null)
    120.             {
    121.                 tenticleShock = GameObject.Find("RoadKillRodney").GetComponent<TenticleShock>();
    122.  
    123.             }
    124.  
    125.  
    126.      
    127.  
    128.  
    129.         if (rigidBody ==null)
    130.         {
    131.             rigidBody = GetComponent<Rigidbody>();
    132.  
    133.         }
    134.  
    135.         if (ballSpawn == null)
    136.         {
    137.             ballSpawn = GetComponent<BallSpawn>();
    138.         }
    139.  
    140.  
    141.         if (footdoDamage ==null)
    142.         {
    143.             footdoDamage = GetComponent<FootDoDamage>();
    144.  
    145.         }
    146.  
    147.  
    148.         if(characterStats == null)
    149.         {
    150.             characterStats = GetComponent<CharacterStats>();
    151.  
    152.         }
    153.  
    154.         if (gameManager ==null)
    155.         {
    156.             gameManager = GetComponent<GameManager>();
    157.  
    158.         }
    159.        
    160.    
    161.         playerController = GetComponent<PlayerController>();
    162.         //int atkRange = Random.Range(1, 5);
    163.         // anim.SetInteger("Atk", atkRange);
    164.         attknumber = Random.Range(0,4);
    165.        
    166.         if (leokick == null)
    167.         {
    168.             leokick = GetComponent<AudioSource>();
    169.  
    170.         }
    171.  
    172.         if (leoTrailAttack == null)
    173.         {
    174.             leoTrailAttack = GetComponent<LeonardoAttackTrailScript>();
    175.  
    176.         }
    177.  
    178.         if (controller == null)
    179.         {
    180.             controller = GetComponent<CharacterController>();
    181.         }
    182.  
    183.         anim = GetComponent<Animator>();
    184.  
    185.         if (attackSound == null)
    186.         attackSound = GetComponent<AudioSource>();
    187.  
    188.         if(shellShock ==null)
    189.         shellShock = GetComponent<AudioSource>();
    190.  
    191.         if(leokick == null)
    192.         leokick = GetComponent<AudioSource>();
    193.  
    194.  
    195.        
    196.     }
    197.     ////Giving 3 seconds for game over music and stage clear to play before playing this.
    198.     public void LeoWinAnim()
    199.     {
    200.         if(toraDoDamage.toraHealth <=0)
    201.             {
    202.             StartCoroutine(LeoWinRoutine());
    203.             //controller.enabled = false;
    204.             //capsuleCollider.enabled = false;
    205.            // playerController.enabled = false;
    206.             print("Win animplaying.");
    207.             }
    208.     }
    209.  
    210.     IEnumerator LeoWinRoutine()
    211.     {  ////Giving 3 seconds for game over music and stage clear to play before playing this.
    212.         yield return new WaitForSeconds(3f);
    213.         anim.SetBool("win", true);
    214.    
    215.  
    216.     }
    217.  
    218.  
    219.  
    220.     // Update is called once per frame
    221.     void FixedUpdate ()
    222.     {
    223.         #region DesktopControls
    224.             TurtleMovement();
    225.             Attack();
    226.             Jump();
    227.             JumpKick();
    228.             SpecialAttack();
    229.         #endregion
    230.  
    231.         #region MobileControls
    232.         //Mobile Movement//
    233.         MobileJump();
    234.         TurtleMobileMovement();
    235.         MobileAttack();
    236.         //MobileSpecialAttack();
    237.         //Mobile Movement//
    238.         #endregion
    239.  
    240.         //StartCoroutine(AttackSound());
    241.     }
    242.  
    243.  
    244.     IEnumerator StunWhileHit()
    245.     {
    246.         controller.enabled = false; //Disable character when hit for two seconds
    247.         playerController.enabled = false;//Disable character when hit for two seconds
    248.         anim.SetBool("hit", true);
    249.         yield return new WaitForSeconds(.3f);
    250.         anim.SetBool("hit", false);
    251.         controller.enabled = true; //Enable Character after it has been disabled.
    252.         playerController.enabled = true; //Enable Character after it has been disabled.
    253.         print("Leo is stunned while being hit.");
    254.     }
    255.  
    256.     void OnTriggerEnter(Collider other)
    257.     {
    258.         //cleaning up leonardo hit code
    259.         //Tora Attack Code
    260.         if (other.tag =="toraattack"  ||  other.tag == "toraglacier" )
    261.             {
    262.  
    263.                 characterStats.leoHealthSlider.value = characterStats.healthLeo;
    264.                 characterStats.healthLeo -= characterStats.bossTackleDamge;
    265.                 anim.SetBool("hit1", true);
    266.                 print("Leo is gettinghit with attack or glacier");
    267.  
    268.                 if (characterStats.healthLeo <= 0)
    269.                 {
    270.                 characterStats.leoHealthSlider.value = characterStats.healthLeo;
    271.  
    272.                 playerController.LeonardoDeath(); // Calling death method
    273.                     characterStats.LeoLives();
    274.  
    275.                     StartCoroutine(LeoRespawnRoutine());
    276.  
    277.                     print("Leonardo is dead.");
    278.                 }
    279.  
    280.         }
    281.         else
    282.             {
    283.                 anim.SetBool("hit", false);
    284.                 anim.SetBool("idle", true);
    285.             }
    286.  
    287.         //Tora Attack Code
    288.  
    289.  
    290.         /////FootSolderAttackCopyJust InCase//////////////////////////////////////
    291.         /*
    292.         if (other.tag == "lfistattack" || other.tag == "rfootattack" || other.tag == "footkatana")
    293.         {
    294.             print("foot punched us.");
    295.             if (GameObject.Find("FootSolder(Clone)") && GameObject.Find("FootSolderSword"))
    296.             {
    297.                 characterStats.leoHealthSlider.value = characterStats.healthLeo;
    298.                 characterStats.healthLeo -= characterStats.damageTakenLeo;
    299.                 anim.SetBool("hit", true);
    300.                 if (characterStats.healthLeo <= 0)
    301.                 {
    302.                     characterStats.leoHealthSlider.value = characterStats.healthLeo;
    303.  
    304.                     playerController.LeonardoDeath(); // Calling death method
    305.                     characterStats.LeoLives();
    306.  
    307.                     StartCoroutine(LeoRespawnRoutine());
    308.  
    309.                     print("Leonardo is dead.");
    310.                 }
    311.  
    312.                 print("Foot is hitting us.");
    313.             }
    314.  
    315.             else
    316.             {
    317.                 anim.SetBool("hit", false);
    318.                 anim.SetBool("idle", true);
    319.             }
    320.  
    321.         }
    322.         */
    323.         /////FootSolderAttackCopyJust InCase//////////////////////////////////////
    324.  
    325.  
    326.         if (other.tag == "lfistattack" || other.tag == "rfootattack"  || other.tag == "footkatana")
    327.         {
    328.          
    329.                 characterStats.leoHealthSlider.value = characterStats.healthLeo;
    330.             //characterStats.healthLeo -= characterStats.damageTakenLeo;// testing damage method in characterstats.
    331.               characterStats.DamageLeo(2);
    332.                 anim.SetBool("hit", true);
    333.             if (characterStats.healthLeo <= 0)
    334.             {
    335.                 characterStats.leoHealthSlider.value = characterStats.healthLeo;
    336.  
    337.                 playerController.LeonardoDeath(); // Calling death method
    338.                 characterStats.LeoLives();
    339.  
    340.                 StartCoroutine(LeoRespawnRoutine());
    341.             }
    342.             else
    343.             {
    344.                 anim.SetBool("hit", false);
    345.                 anim.SetBool("idle", true);
    346.             }
    347.  
    348.         }
    349.         /////FootSolderAttack//////////////////////////////////////
    350.  
    351.         //MetalBall
    352.         if(other.tag=="metalball")
    353.         {
    354.             playerController.LeonardoDeath(); // Calling death method
    355.             characterStats.LeoLives();
    356.  
    357.             StartCoroutine(LeoRespawnRoutine());
    358.             Debug.Log("Leo is hit with MetalBall.");
    359.         }
    360.         //MetalBall
    361.  
    362.         ////Snow Plow
    363.         if (other.tag == "snowplow" )
    364.         {
    365.  
    366.             characterStats.leoHealthSlider.value = characterStats.healthLeo;
    367.             characterStats.healthLeo -= characterStats.snowPlowDamage;
    368.             anim.SetBool("hit1", true);
    369.             print("Leo hit with SnowPlow");
    370.  
    371.             if (characterStats.healthLeo <= 0)
    372.             {
    373.                 characterStats.leoHealthSlider.value = characterStats.healthLeo;
    374.  
    375.                 playerController.LeonardoDeath(); // Calling death method
    376.                 characterStats.LeoLives();
    377.  
    378.                 StartCoroutine(LeoRespawnRoutine());
    379.  
    380.                 print("Leonardo is dead.");
    381.             }
    382.  
    383.         }
    384.         else
    385.         {
    386.             anim.SetBool("hit", false);
    387.             anim.SetBool("idle", true);
    388.         }
    389.         ////Snow Plow
    390.  
    391.  
    392.  
    393.  
    394.  
    395.  
    396.  
    397.  
    398.  
    399.  
    400.  
    401.     }//TriggerMETHOD
    402.  
    403.  
    404.  
    405.  
    406.     public void LeonardoDeath()
    407.     {
    408.         shellShock.Play();
    409.         death.Play();
    410.        
    411.         anim.SetTrigger("shellshock");
    412.         controller.enabled = false;
    413.         capsuleCollider.enabled = false;
    414.         playerController.enabled = false;
    415.  
    416.     }//LeoDeath
    417.  
    418.    
    419.  
    420.  
    421.     public void RespawnPlayer()
    422.     {
    423.  
    424.         characterStats.leoLife.text = "x " + characterStats.lifeLeo.ToString();
    425.         //characterStats.leoLife.text = "x" + GetComponent<CharacterStats>().leoLife;
    426.         //characterStats.leoHealthSlider.value = characterStats.healthLeo;
    427.  
    428.         //characterStats.leoHealthSlider.value = characterStats.healthLeo;
    429.         characterStats.LeoHealth();
    430.        
    431.         transform.position = respawnPosition;
    432.         controller.enabled = true;
    433.         playerController.enabled = true;
    434.         anim.SetTrigger("wearerespawned");
    435.  
    436.     }//RESPAWNPLAYER
    437.    
    438.   IEnumerator LeoRespawnRoutine()
    439.     {
    440.         yield return new WaitForSeconds(1f);
    441.         RespawnPlayer();
    442.         print("Running LeoRespawnRoutine()");
    443.  
    444.     }
    445.  
    446.    
    447.  
    448.     public void JumpKick()
    449.     {
    450.         // character must be already in jumping then press the 3 button to perform jumpkick
    451.        if(!controller.isGrounded && Input.GetKeyDown(KeyCode.Joystick1Button2))
    452.         {
    453.             leoTrailAttack.rend.enabled = true;
    454.             anim.SetBool("jumpkick", true);
    455.             anim.SetBool("kick",false);
    456.            
    457.             lFootAttackBox.gameObject.SetActive(true);
    458.             lKatanaAttackBox.gameObject.SetActive(true);
    459.             rKatanaAttackBox.gameObject.SetActive(true);
    460.            
    461.             print("Performing JumpKick.");
    462.         }
    463.  
    464.         if (Input.GetKeyUp(KeyCode.Joystick1Button2))
    465.         {
    466.            
    467.             lFootAttackBox.gameObject.SetActive(false);
    468.             lKatanaAttackBox.gameObject.SetActive(false);
    469.             rKatanaAttackBox.gameObject.SetActive(false);
    470.          
    471.  
    472.         }
    473.         //KeyBoard Controller
    474.         // character must be already in jumping then press the 3 button to perform jumpkick
    475.         if (!controller.isGrounded && Input.GetKeyDown(KeyCode.K))
    476.         {
    477.             leoTrailAttack.rend.enabled = true;
    478.             anim.SetBool("jumpkick", true);
    479.             anim.SetBool("kick", false);
    480.  
    481.             lFootAttackBox.gameObject.SetActive(true);
    482.             lKatanaAttackBox.gameObject.SetActive(true);
    483.             rKatanaAttackBox.gameObject.SetActive(true);
    484.  
    485.             print("Performing JumpKick.");
    486.         }
    487.  
    488.         if (Input.GetKeyUp(KeyCode.K))
    489.         {
    490.  
    491.             lFootAttackBox.gameObject.SetActive(false);
    492.             lKatanaAttackBox.gameObject.SetActive(false);
    493.             rKatanaAttackBox.gameObject.SetActive(false);
    494.  
    495.  
    496.         }
    497.  
    498.  
    499.  
    500.     }
    501.  
    502.     IEnumerator AttackSound()
    503.     {
    504.         yield return new WaitForSeconds(0.5f);
    505.         attackSound.Play();
    506.         print("Running Attack Coroutine.");
    507.     }
    508.  
    509.     public void TurtleMobileMovement()
    510.     {
    511.         Vector2 input = new Vector2(CrossPlatformInputManager.GetAxisRaw("Horizontal"), CrossPlatformInputManager.GetAxisRaw("Vertical"));
    512.         Vector2 inputDir = input.normalized;
    513.  
    514.         //keeps from character snapping after turning
    515.         if (inputDir != Vector2.zero)
    516.         {
    517.             float targetRotation = Mathf.Atan2(inputDir.x, inputDir.y) * Mathf.Rad2Deg;
    518.             transform.eulerAngles = Vector3.up * Mathf.SmoothDampAngle(transform.eulerAngles.y, targetRotation, ref turnSmoothVelocity, turnSmoothTime);
    519.  
    520.             //using this older method as it removes errors
    521.             transform.eulerAngles = Vector3.up * Mathf.Atan2(inputDir.x, inputDir.y) * Mathf.Rad2Deg;
    522.  
    523.        
    524.         }
    525.  
    526.    
    527.  
    528.         bool running = (Input.GetKey(KeyCode.Joystick1Button0));
    529.         float targetSpeed = ((running) ? runSpeed : walkSpeed) * inputDir.magnitude;
    530.         currentSpeed = Mathf.SmoothDamp(currentSpeed, targetSpeed, ref speedSmoothVelocity, speedSmoothTime);
    531.  
    532.  
    533.         //////////////////////////////////////////////////////////////////////////////////////
    534.         //Moves forward
    535.  
    536.         //Turning off to see if i can have the vibration off with rigidbody instead of transform.
    537.         transform.Translate(transform.forward * currentSpeed * Time.deltaTime, Space.World);
    538.         //Turning off to see if i can have the vibration off with rigidbody instead of transform.
    539.  
    540.  
    541.         //Testing to see if this removes the vibration when colliding on collider
    542.         //So far it works
    543.         //rigidBody.AddForce(Vector3.forward * currentSpeed * Time.deltaTime);
    544.  
    545.  
    546.         float animationSpeedPercent = ((running) ? 1 : .5f) * inputDir.magnitude;
    547.         anim.SetFloat("speedPercent", animationSpeedPercent, speedSmoothTime, Time.deltaTime);
    548.  
    549.  
    550.  
    551.         //Gravity////////////////////////////////////////
    552.         velocityY += Time.deltaTime * gravity;
    553.         Vector3 velocity = transform.forward * currentSpeed + Vector3.up * velocityY;
    554.  
    555.         controller.Move(velocity * Time.deltaTime);
    556.         currentSpeed = new Vector2(controller.velocity.x, controller.velocity.z).magnitude;
    557.  
    558.  
    559.         if (controller.isGrounded)
    560.         {
    561.             velocityY = 0;
    562.         }
    563.         //Gravity////////////////////////////////////////
    564.     }
    565.  
    566.     public void TurtleMovement()
    567.     {
    568.         Vector2 input = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"));
    569.         Vector2 inputDir = input.normalized;
    570.        
    571.         //keeps from character snapping after turning
    572.         if (inputDir != Vector2.zero)
    573.         {
    574.             float targetRotation = Mathf.Atan2(inputDir.x, inputDir.y) * Mathf.Rad2Deg;
    575.             transform.eulerAngles = Vector3.up * Mathf.SmoothDampAngle(transform.eulerAngles.y, targetRotation, ref turnSmoothVelocity, turnSmoothTime);
    576.  
    577.             //using this older method as it removes errors
    578.             transform.eulerAngles = Vector3.up * Mathf.Atan2(inputDir.x, inputDir.y) * Mathf.Rad2Deg;
    579.  
    580.             //used to be this: transform.eulerAngles = Vector3.up * Mathf.SmoothDampAngle(transform.eulerAngles.y, targetRotation, ref turnSmoothVelocity, turnSmoothTime);
    581.             //Vector3 CleanInput = Vector3.up * Mathf.SmoothDampAngle(transform.eulerAngles.y, targetRotation, ref turnSmoothVelocity, turnSmoothTime);
    582.            // if (float.IsNaN(CleanInput.x)) CleanInput.x = 0;
    583.            // if (float.IsNaN(CleanInput.y)) CleanInput.y = 0;
    584.            // if (float.IsNaN(CleanInput.z)) CleanInput.z = 0;
    585.            // transform.eulerAngles = CleanInput;
    586.             //is turnSmoothVelocity initialized to a value?
    587.         }
    588.  
    589.  
    590.        
    591.         bool running = (Input.GetKey(KeyCode.Joystick1Button0));
    592.         float targetSpeed = ((running) ? runSpeed : walkSpeed) * inputDir.magnitude;
    593.         currentSpeed = Mathf.SmoothDamp(currentSpeed, targetSpeed, ref speedSmoothVelocity, speedSmoothTime);
    594.  
    595.  
    596.         //////////////////////////////////////////////////////////////////////////////////////
    597.         //Moves forward
    598.        
    599.         transform.Translate(transform.forward *currentSpeed * Time.deltaTime, Space.World);
    600.          //UpdatedCode make the float.isNaN into a capital float.IsNaN
    601.          ////this cleans the vectors and resets to zero
    602.         //Vector3 TransflateValue = transform.forward * currentSpeed * Time.deltaTime;
    603.       //  if (float.IsNaN(TransflateValue.x)) TransflateValue.x = 0;
    604.       //  if (float.IsNaN(TransflateValue.y)) TransflateValue.y = 0;
    605.       //  if (float.IsNaN(TransflateValue.z)) TransflateValue.z = 0;
    606.       //  transform.Translate(TransflateValue, Space.World);
    607.         ////////////////////////////////////////////////////////
    608.  
    609.  
    610.         float animationSpeedPercent = ((running) ? 1 : .5f) * inputDir.magnitude;
    611.         anim.SetFloat("speedPercent", animationSpeedPercent, speedSmoothTime, Time.deltaTime);
    612.          
    613.  
    614.  
    615.         //Gravity////////////////////////////////////////
    616.         velocityY += Time.deltaTime * gravity;
    617.         Vector3 velocity = transform.forward * currentSpeed + Vector3.up * velocityY;
    618.  
    619.         controller.Move(velocity * Time.deltaTime);
    620.         currentSpeed = new Vector2(controller.velocity.x, controller.velocity.z).magnitude;
    621.  
    622.  
    623.             if (controller.isGrounded)
    624.             {
    625.                 velocityY = 0;
    626.             }
    627.         //Gravity////////////////////////////////////////
    628.  
    629.  
    630.     }
    631.  
    632.     public void Attack()
    633.     {
    634.         if(Input.GetKeyDown(KeyCode.Joystick1Button2))
    635.         {
    636.             leokick.Play();
    637.  
    638.             leoTrailAttack.rend.enabled = true;
    639.             anim.SetBool("kick" ,true);
    640.        
    641.             rFootAttackBox.gameObject.SetActive(true);
    642.             lKatanaAttackBox.gameObject.SetActive(true);
    643.             rKatanaAttackBox.gameObject.SetActive(true);
    644.             //anim.SetInteger("attknumber", attknumber);
    645.             anim.SetBool("idle", false);
    646.         }
    647.    
    648.        
    649.         if (Input.GetKeyUp(KeyCode.Joystick1Button2))
    650.         {
    651.            leoTrailAttack.rend.enabled = false;
    652.             //anim.SetInteger("attknumber", attknumber);
    653.  
    654.             anim.SetBool("kick", false);
    655.             //weapon attack Hiding
    656.             lKatanaAttackBox.gameObject.SetActive(false);
    657.             rKatanaAttackBox.gameObject.SetActive(false);
    658.             rFootAttackBox.gameObject.SetActive(false);
    659.  
    660.             anim.SetBool("idle", true);
    661.         }
    662.         //KeyBoard Controller
    663.         if (Input.GetKeyDown(KeyCode.K))
    664.         {
    665.             leokick.Play();
    666.  
    667.            leoTrailAttack.rend.enabled = true;
    668.             anim.SetBool("kick", true);
    669.  
    670.             rFootAttackBox.gameObject.SetActive(true);
    671.             lKatanaAttackBox.gameObject.SetActive(true);
    672.             rKatanaAttackBox.gameObject.SetActive(true);
    673.             //anim.SetInteger("attknumber", attknumber);
    674.             anim.SetBool("idle", false);
    675.         }
    676.  
    677.  
    678.         if (Input.GetKeyUp(KeyCode.K))
    679.         {
    680.             leoTrailAttack.rend.enabled = false;
    681.             //anim.SetInteger("attknumber", attknumber);
    682.  
    683.             anim.SetBool("kick", false);
    684.             //weapon attack Hiding
    685.             lKatanaAttackBox.gameObject.SetActive(false);
    686.             rKatanaAttackBox.gameObject.SetActive(false);
    687.             rFootAttackBox.gameObject.SetActive(false);
    688.  
    689.             anim.SetBool("idle", true);
    690.         }
    691.  
    692.  
    693.     }
    694.     public void SpecialAttack()
    695.     {
    696.         if(Input.GetKeyDown(KeyCode.Joystick1Button2) && (Input.GetKey(KeyCode.Joystick1Button1)))
    697.             {
    698.        
    699.               CancelInvoke("jump");
    700.               anim.SetBool("jump", false);
    701.               canJump = false;
    702.  
    703.             print("we are pressing both buttons at the same time.");
    704.             }
    705.         else
    706.         {
    707.             anim.SetBool("idle", true);
    708.             canJump = true;
    709.  
    710.  
    711.         }
    712.         //KeyBoard
    713.         if (Input.GetKeyDown(KeyCode.K) && (Input.GetKey(KeyCode.J)))
    714.         {
    715.  
    716.             CancelInvoke("jump");
    717.             anim.SetBool("jump", false);
    718.             canJump = false;
    719.  
    720.             print("we are pressing both buttons at the same time.");
    721.         }
    722.         else
    723.         {
    724.             anim.SetBool("idle", true);
    725.             canJump = true;
    726.  
    727.  
    728.         }
    729.     }
    730.  
    731.  
    732.  
    733.     public void MobileAttack()
    734.     {
    735.         if (CrossPlatformInputManager.GetButtonDown("Attack"))
    736.         {
    737.             leokick.Play();
    738.  
    739.             leoTrailAttack.rend.enabled = true;
    740.             anim.SetBool("kick", true);
    741.  
    742.             rFootAttackBox.gameObject.SetActive(true);
    743.             lKatanaAttackBox.gameObject.SetActive(true);
    744.             rKatanaAttackBox.gameObject.SetActive(true);
    745.             //anim.SetInteger("attknumber", attknumber);
    746.             anim.SetBool("idle", false);
    747.         }
    748.  
    749.         else
    750.         {
    751.             anim.SetBool("idle", true);
    752.             anim.SetBool("kick", false);
    753.             leoTrailAttack.rend.enabled = false;
    754.             rFootAttackBox.gameObject.SetActive(false);
    755.             lKatanaAttackBox.gameObject.SetActive(false);
    756.             rKatanaAttackBox.gameObject.SetActive(false);
    757.             print("Mobile Buttons not pressed.");
    758.  
    759.         }
    760.  
    761.     }
    762.  
    763.     public void MobileSpecialAttack()
    764.     {
    765.         if (CrossPlatformInputManager.GetButtonDown("Attack") && CrossPlatformInputManager.GetButtonDown("Jump"))
    766.         {
    767.             leokick.Play();
    768.  
    769.             leoTrailAttack.rend.enabled = true;
    770.             anim.SetBool("kick", true);
    771.  
    772.             rFootAttackBox.gameObject.SetActive(true);
    773.             lKatanaAttackBox.gameObject.SetActive(true);
    774.             rKatanaAttackBox.gameObject.SetActive(true);
    775.             //anim.SetInteger("attknumber", attknumber);
    776.             anim.SetBool("idle", false);
    777.         }
    778.         else
    779.         {
    780.             anim.SetBool("idle", true);
    781.             anim.SetBool("kick", false);
    782.             leoTrailAttack.rend.enabled = false;
    783.             rFootAttackBox.gameObject.SetActive(false);
    784.             lKatanaAttackBox.gameObject.SetActive(false);
    785.             rKatanaAttackBox.gameObject.SetActive(false);
    786.             print("Mobile Buttons not pressed.");
    787.  
    788.         }
    789.  
    790.     }
    791.     public void MobileJump()
    792.  
    793.     {
    794.         if(controller.isGrounded && canJump == true)
    795.         {
    796.             if (CrossPlatformInputManager.GetButtonDown("Jump"))
    797.             {
    798.                 //leoJump.Play();
    799.                 anim.SetBool("jump", true);
    800.                 float jumpVelocity = Mathf.Sqrt(-2 * gravity * jumpHeight);
    801.                 velocityY = jumpVelocity;
    802.  
    803.                 //print("We are Jumping");
    804.             }
    805.             else
    806.             {
    807.                 anim.SetBool("jump", false);
    808.             }
    809.         }
    810.      
    811.     }
    812.  
    813.     public void Jump()
    814.    {
    815.         if (Input.GetKey(KeyCode.Joystick1Button1))
    816.         {
    817.             //cameraFollow.enabled = false;//Turning off camera follow
    818.             if(controller.isGrounded && canJump ==true )
    819.             {
    820.                 //leoJump.Play();
    821.                 anim.SetBool("jump", true);
    822.                 float jumpVelocity = Mathf.Sqrt(-2 * gravity * jumpHeight);
    823.                 velocityY = jumpVelocity;
    824.  
    825.                 //print("We are Jumping");
    826.             }
    827.         }
    828.  
    829.  
    830.         if (Input.GetKeyUp(KeyCode.Joystick1Button1))
    831.         {
    832.             //cameraFollow.enabled = false;//Turning off camera follow
    833.  
    834.             anim.SetBool("jump", false);
    835.             anim.SetBool("idle", true);
    836.  
    837.         }
    838.         // KeyBoard Jump
    839.         if (Input.GetKey(KeyCode.Space))
    840.         {
    841.             //cameraFollow.enabled = false;//Turning off camera follow
    842.             if (controller.isGrounded && canJump == true)
    843.             {
    844.                 //leoJump.Play();
    845.                 anim.SetBool("jump", true);
    846.                 float jumpVelocity = Mathf.Sqrt(-2 * gravity * jumpHeight);
    847.                 velocityY = jumpVelocity;
    848.  
    849.                 //print("We are Jumping");
    850.             }
    851.         }
    852.  
    853.  
    854.         if (Input.GetKeyUp(KeyCode.Space))
    855.         {
    856.             //cameraFollow.enabled = false;//Turning off camera follow
    857.  
    858.             anim.SetBool("jump", false);
    859.             anim.SetBool("idle", true);
    860.  
    861.         }
    862.  
    863.     }
    864.  
    865.     //This lets you control the character when in air via a rangeslider put in a public float further up.
    866.     float GetModifiedSmoothTime(float smoothTime)
    867.     {
    868.         if(controller.isGrounded)
    869.         {
    870.             return smoothTime;
    871.         }
    872.  
    873.         if(airControlPercent == 0)
    874.         {
    875.             return float.MaxValue;
    876.         }
    877.         return smoothTime / airControlPercent;
    878.     }
    879.  
    880.  
    881.  
    882.    
    883.  
    884.  
    885.  
    886. }
    887.