Search Unity

Level updater

Discussion in 'Scripting' started by Deleted User, Jul 5, 2015.

  1. Deleted User

    Deleted User

    Guest

    I have this script that checks how many cubes were collected and then the amount collected is can be used since the the count variable is a getter.
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3. using System.Collections;
    4.  
    5. public class PlayerController : MonoBehaviour {
    6.  
    7.     /////////////////
    8.     /// Variables ///
    9.     /////////////////
    10.     private int count = 0;
    11.         .
    12.         .
    13.         .
    14.     public int Count {
    15.         get {
    16.             return count;
    17.         }
    18.     }
    19.  
    20.         .
    21.         .
    22.         .
    23.  
    24.     /////////////////////////////
    25.     /// Change score on touch ///
    26.     /////////////////////////////
    27.     void OnTriggerEnter(Collider other) {
    28.         if(other.gameObject.CompareTag("PickUp")) {
    29.             other.gameObject.SetActive(false);
    30.             count++;
    31.             UpdateScore();
    32.         }
    33.     }
    34.  
    35.     ///////////////////////////
    36.     /// Collected all cubes ///
    37.     ///////////////////////////
    38.     void UpdateScore() {
    39.         countText.text = "Score: " + count.ToString();
    40.         if(count >= howManyToCollect.Length) {
    41.             allCollectedText.text = "All cubes collected, roll to the portal!";
    42.         }
    43.     }
    44.  
    45. }
    I also have this script that is suppose to load the next level after checking if the number of cubes in the level and the number of cubes collected (used the count getter).
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class UpdateScene : MonoBehaviour {
    5.  
    6.     private GameObject[] howManyNeeded;
    7.  
    8.     ///////////////////////////////////
    9.     /// How many needed to continue ///
    10.     ///////////////////////////////////
    11.     void Start() {
    12.         howManyNeeded = GameObject.FindGameObjectsWithTag("PickUp");
    13.     }
    14.  
    15.     ///////////////////////////////////////////////////////////////////
    16.     /// Change level if player touches portal & all cubes collected ///
    17.     ///////////////////////////////////////////////////////////////////
    18.     void OnTriggerEnter(Collider other) {
    19.         PlayerController countFromPlayerController = new PlayerController();
    20.         if(other.gameObject.CompareTag("Portal") && howManyNeeded.Length >= countFromPlayerController.Count) {
    21.             Application.LoadLevel(Application.loadedLevel + 1);
    22.         }
    23.     }
    24.  
    25. }
    I checked the console and it doesn't show any error.

    Is it even possible to change the value of a getter in the same script? Or do I have to use another variable in the UpdateScene script in the same way I used the count in the PlayerController?
     
  2. steego

    steego

    Joined:
    Jul 15, 2010
    Posts:
    969
    Code (csharp):
    1. PlayerController countFromPlayerController = new PlayerController();
    This will create a new PlayerController each time OnTriggerEnter is called, and this PlayerController will have the default values, so your count variable will always be 0.

    Instead, you want a reference to the PlayerController that already exists on your player. The easiest way to do this is to set it in the inspector, just add
    Code (csharp):
    1. public PlayerController countFromPlayerController;
    in your UpdateScene script, and then remove the line PlayerController countFromPlayerController = new PlayerController(); and then drag your player into the countFromPlayerController field in the inspector.

    There are also other ways to do this, you could get the PlayerController from script with FindObjectOfType or with static variables or a singleton, but try with a public variable and the inspector first.
     
    Deleted User likes this.
  3. Deleted User

    Deleted User

    Guest

    I found out I attached the UpdateScene script to the wrong object.

    I changed it and the script I wrote before works but this time I get the warning:
    You are trying to create a MonoBehaviour using the 'new' keyword. This is not allowed. MonoBehaviours can only be added using AddComponent(). Alternatively, your script can inherit from ScriptableObject or no base class at all
    UnityEngine.MonoBehaviour:.ctor()
    PlayerController:.ctor()
    UpdateScene:OnTriggerEnter(Collider) (at Assets/Scripts/UpdateScene.cs:21)

    (I get the warning everytime I pick up any of the PickUps)

    If I change the script to what you suggested I get the error:

    Object refrence not set to an instance of an object

    It works without any error or warning when I use another variable in the same way I use the count variable in the PlayerController script (I can just do that but I want to use a different way since I'm still learning C#).
     
  4. steego

    steego

    Joined:
    Jul 15, 2010
    Posts:
    969
    This is what I was saying, you shouldn't be doing PlayerController countFromPlayerController = new PlayerController();

    Did you remember to assign it in the inspector in the editor?
     
    Deleted User likes this.
  5. Deleted User

    Deleted User

    Guest

    Ok thank you it works fine now :)
     
    steego likes this.