Search Unity

Character Changing Scripts not working - No errors

Discussion in 'Scripting' started by MaskyDEV, Nov 29, 2014.

  1. MaskyDEV

    MaskyDEV

    Joined:
    Nov 16, 2014
    Posts:
    10
    I have been watching tutorials by TheFaceGrabber, and I have been following along with his scripts. Here are two scripts that are supposed to be used to switch characters. Also, the camera is supposed to zoom out, and then go to the selected player and zoom back in.
    Game Manager:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4.  
    5.     public class GameManager : MonoBehaviour {
    6.     public List<Character> Characters = new List<Character>();
    7.     bool ShowCharWheel;
    8.     public int SelectedCharacter;
    9.     int LastCharacter;
    10.     public static GameManager Instance;
    11.     public bool CanShowSwitch = true;
    12.  
    13.     void Awake()
    14.     {
    15.         Instance = this;
    16.         foreach (Character c in Characters)
    17.         {
    18.             c.Instance = Instantiate (c.PlayerPrefab, c.HomeSpawn.position, c.HomeSpawn.rotation) as GameObject;
    19.         }
    20.         ChangeCharacterStart(Characters[PlayerPrefs.GetInt("SelectedChar")]);
    21.     }
    22.     // Use this for initialization
    23.     void Start ()
    24.     {
    25.  
    26.  
    27.     }
    28.    
    29.     // Update is called once per frame
    30.     void Update () {
    31.         if (Input.GetKey (KeyCode.C))
    32.         {
    33.             ShowCharWheel = true;
    34.         }
    35.             else
    36.         {
    37.             ShowCharWheel = false;
    38.         }
    39.         Characters [SelectedCharacter].Instance.GetComponent<PlayerController> ().CanPlay = true;
    40.         Camera.main.GetComponent<SmoothFollow> ().target = Characters[SelectedCharacter].Instance.transform;
    41.    
    42.     }
    43.  
    44.     void ChangeCharacterStart(Character c)
    45.     {
    46.         LastCharacter = SelectedCharacter;
    47.         SelectedCharacter = Characters.IndexOf (c);
    48.         Characters [LastCharacter].Instance.GetComponent<PlayerController> ().CanPlay = false;
    49.         Characters [SelectedCharacter].Instance.GetComponent<PlayerController> ().CanPlay = true;
    50.         Camera.main.GetComponent<SmoothFollow> ().target = Characters[SelectedCharacter].Instance.transform;
    51.         PlayerPrefs.SetInt ("SelectedChar", SelectedCharacter);
    52.     }
    53.  
    54.     void ChangeCharacter(Character c)
    55.     {
    56.         CanShowSwitch = false;
    57.         SequenceManager.Instance.StartCoroutine ("DoCharSwitch", c);
    58.         LastCharacter = SelectedCharacter;
    59.         SelectedCharacter = Characters.IndexOf (c);
    60.         Characters [LastCharacter].Instance.GetComponent<PlayerController> ().CanPlay = false;
    61.         Characters [SelectedCharacter].Instance.GetComponent<PlayerController> ().CanPlay = true;
    62.         //Camera.main.GetComponent<SmoothFollow> ().target = Characters[SelectedCharacter].Instance.transform;
    63.         PlayerPrefs.SetInt ("SelectedChar", SelectedCharacter);
    64.     }
    65.  
    66.     void OnGUI()
    67.     {
    68.         if (ShowCharWheel && CanShowSwitch)
    69.         {
    70.             GUILayout.BeginArea(new Rect(Screen.width-64, Screen.height-192,64,192));
    71.             foreach(Character c in Characters)
    72.             {
    73.                 if(GUILayout.Button(c.Icon,GUILayout.Width(64),GUILayout.Height(64)))
    74.                 {
    75.                     ChangeCharacter(c);
    76.                 }
    77.             }
    78.             GUILayout.EndArea();
    79.         }
    80.     }
    81. }
    82.  
    83. [System.Serializable]
    84. public class Character
    85. {
    86.     public string Name;
    87.     public Texture2D Icon;
    88.     public GameObject PlayerPrefab;
    89.     public GameObject Instance;
    90.     public Transform HomeSpawn;
    91. }
    Sequence Manager:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class SequenceManager : MonoBehaviour {
    5.     public CharacterTransition CharSwitch;
    6.  
    7.     public static SequenceManager Instance;
    8.  
    9.     void Awake()
    10.     {
    11.         Instance = this;
    12.     }
    13.  
    14.     public IEnumerator DoCharSwitch(Character c)
    15.     {
    16.         int i = 0;
    17.         while (i > 3)
    18.         {
    19.             if (i == 0)
    20.             {
    21.                 CharSwitch.Follower.height = CharSwitch.Height;
    22.                 while(Mathf.Round(CharSwitch.Follower.transform.position.y) != CharSwitch.Height)
    23.                     yield return new WaitForEndOfFrame();
    24.             }
    25.             if (i == 1)
    26.             {
    27.                 Vector3 pos = new Vector3(c.Instance.transform.position.x, CharSwitch.Height, c.Instance.transform.position.z);
    28.                 CharSwitch.Follower.transform.position = Vector3.Lerp(CharSwitch.Follower.transform.position,pos,CharSwitch.Speed*Time.deltaTime);
    29.  
    30.                 if(CharSwitch.Follower.transform.position != pos)
    31.                     yield return new WaitForEndOfFrame();
    32.             }
    33.  
    34.             if (i == 2)
    35.             {
    36.                 Camera.main.GetComponent<SmoothFollow> ().target = c.Instance.transform;
    37.                 CharSwitch.Follower.height = 2;
    38.                 GameManager.Instance.CanShowSwitch = true;
    39.                 if ((int)CharSwitch.Follower.transform.position.y != 2)
    40.                     yield return new WaitForEndOfFrame();
    41.             }
    42.             i++;
    43.         }
    44.         StopCoroutine ("DoCharSwitch");
    45.         yield return null;
    46.     }
    47.     // Use this for initialization
    48.     void Start () {
    49.    
    50.     }
    51.    
    52.     // Update is called once per frame
    53.     void Update () {
    54.    
    55.     }
    56. }
    57.  
    58. [System.Serializable]
    59. public class CharacterTransition
    60. {
    61.     public float Height;
    62.     public float Speed;
    63.     public SmoothFollow Follower;
    64. }
    Any help is appriciated, thank you in advance
     
  2. OpticalOverride

    OpticalOverride

    Joined:
    Jan 13, 2013
    Posts:
    161
    It's late for me, about 20 minutes to sleep but I must say, this is an awful lot of code to simply say "it doesn't work", and hand it to the community to solve. You may get lucky and find someone who's really kind and wants to help, but if no one does, my suggestion is that if you're new to programming you may want to take smaller bites. This is a lot of code for a beginner, if you're not understanding how each piece of the code works, break it down into really small pieces and get each tiny section to work ("play" with the code, if you will) and do this until you understand it better. You will then understand it as whole much better. Either that, or perhaps choose a simpler project to begin learning to program.

    Sorry I can't be of direct help, but again IMO this is quite a lot of code for such a general question.