Search Unity

can you help me?

Discussion in '2D' started by seankellygaller5, Jul 22, 2019.

  1. seankellygaller5

    seankellygaller5

    Joined:
    Jul 13, 2019
    Posts:
    16
    hi! I'm a Student of Darfield High School making a game for my Assesment. however, I've run some issues while I'm making my game.


    first off: collecting gems.
    on the first Level, my character can collect gem on quite easily. here as you can see the Level Manager script records that gem being collected.
    upload_2019-7-22_18-32-59.png

    however, in the second and third level, my character just can't collect the gems. here's the evidence...

    upload_2019-7-22_18-52-26.png
    upload_2019-7-22_18-53-19.png
    as you can see the gems in level 2 and 3 doesn't collect the gems at all even if the player is literary inside of the gem. I don't know what went wrong but could you plz help me out solve this problem plz?
     
  2. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,776
    Do you use layers anywhere? If so, are these are same on each level.
    Does script are attached correctly to Game Objects on each level?
    Do you have correct references in script to relevant level?

    Just some thoughts.
     
  3. seankellygaller5

    seankellygaller5

    Joined:
    Jul 13, 2019
    Posts:
    16
    1. i didn't what type of layers?, the sorting layer or the tag layer?

    2. yes i think so i've triple checked it multiple times but still doesn't work on level 2 and three.

    3. um i don't know could you elobeorate that?
     
  4. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,487
    How do you check that you've collided with a gem? I can only presume the gems have 2D trigger colliders on them and your player has a collider too and that somewhere you have a script (presumably) on the player that has a "OnTriggerEnter2D" and checks if it hits a gem? Assuming that's the case then I also presume you've set in Physics 2D settings the collision matrix that allows gems to collide with the player, again assuming that the player and gems are set to their own layers.

    I very much doubt that if it works in one scene but not the next that the collisions are suddenly not working but it's more likely that the logic behind the gem collection somehow gets disconnected from the level manager.

    It would be best to share how the contact with gems is set-up, what deletes gems and what passes the fact that you've collided with a gem to this level-manager script.
     
  5. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,776
    These layers.
    upload_2019-7-22_15-7-40.png
    But if you haven't used them, then you shouldn't have an issue on that part.

    If you answer @MelvMay post first, this may be better to explain.
    But in brief, you maybe have class with scene1,2,3 and your gems are not assigned to other scenes. Is just pure guess atm.
     
  6. seankellygaller5

    seankellygaller5

    Joined:
    Jul 13, 2019
    Posts:
    16

    thank you for responding this problem and sure thing i will show you what's my coding between my player, the gem, level manager and my game master.

    playermovement script:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerMovement : MonoBehaviour
    6. {
    7.  
    8.     public CharacterController2D controller;
    9.     public Animator animator;
    10.     public float runSpeed = 40f;
    11.  
    12.     float horizontalMove = 0f;
    13.     bool Jump = false;
    14.     bool Crouch = false;
    15.  
    16.     private gameMaster gm;
    17.     public Vector3 respawnPoint;
    18.     public LevelManager GameLevelManager;
    19.     // Update is called once per frame
    20.  
    21.     void Start()
    22.     {
    23.         gm = GameObject.FindGameObjectWithTag("gamemaster").GetComponent<gameMaster>();
    24.         Debug.Log("game master");
    25.         respawnPoint = transform.position;
    26.         GameLevelManager = FindObjectOfType<LevelManager>();
    27.     }
    28.     void Update()
    29.     {
    30.  
    31.         horizontalMove = Input.GetAxisRaw("Horizontal") * runSpeed;
    32.  
    33.         animator.SetFloat("Speed", Mathf.Abs( horizontalMove));
    34.  
    35.         if (Input.GetButtonDown("Jump"))
    36.         {
    37.             Jump = true;
    38.             animator.SetBool("IsJumping", true);
    39.         }
    40.  
    41.         if (Input.GetButtonDown("Crouch"))
    42.         {
    43.             Crouch = true;
    44.         } else if (Input.GetButtonUp("Crouch"))
    45.         {
    46.             Crouch = false;
    47.         }
    48.     }
    49.  
    50.     public void OnLanding()
    51.     {
    52.         animator.SetBool("IsJumping", false);
    53.     }
    54.    public void OnCrouching( bool isCrouching)
    55.     {
    56.         animator.SetBool("IsCrouching", isCrouching);
    57.     }
    58.     // Character Movement controls
    59.     private void FixedUpdate()
    60.     {
    61.         controller.Move(horizontalMove * Time.fixedDeltaTime, Crouch, Jump);
    62.         Jump = false;
    63.     }
    64.  
    65.  
    66.     void OnTriggerEnter2D(Collider2D col)
    67.     {
    68.        
    69.  
    70.         if(col.tag == "falldetector")
    71.         {
    72.             // what will happen if Player enters the fallDetector
    73.             GameLevelManager.Respawn();
    74.         }
    75.        
    76.     }
    77. }
    78.  


    GemScript:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class GemScript : MonoBehaviour
    6. {
    7.  
    8.     private LevelManager gameLevelManager;
    9.     public int gemValue;
    10.     // Start is called before the first frame update
    11.     void Start()
    12.     {
    13.         gameLevelManager = FindObjectOfType<LevelManager>();
    14.     }
    15.  
    16.     // Update is called once per frame
    17.     void Update()
    18.     {
    19.        
    20.     }
    21.  
    22.     void OnTriggerEnter2D(Collider2D col)
    23.     {
    24.     if(col.tag == "Player")
    25.         {
    26.             gameLevelManager.AddGems(gemValue);
    27.             Destroy(gameObject);
    28.             Debug.Log("Score ");
    29.         }
    30.        
    31.  
    32.     }
    33. }
    34.  

    LevelManager:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class LevelManager : MonoBehaviour
    7. {
    8.     public float respawnDelay;
    9.     public PlayerMovement gamePlayer;
    10.     public int gems;
    11.     public Text GemText;
    12.     // Start is called before the first frame update
    13.     void Start()
    14.     {
    15.         gamePlayer = FindObjectOfType<PlayerMovement>();
    16.         GemText.text = "Gems:" + gems;
    17.     }
    18.  
    19.     // Update is called once per frame
    20.     void Update()
    21.     {
    22.        
    23.     }
    24.  
    25.     public void Respawn()
    26.     {
    27.         StartCoroutine("RespawnCoroutine");
    28.      
    29.     }
    30.  
    31.     public IEnumerator RespawnCoroutine()
    32.     {
    33.         gamePlayer.gameObject.SetActive(false);
    34.         yield return new WaitForSeconds(respawnDelay);
    35.         gamePlayer.transform.position = gamePlayer.respawnPoint;
    36.         gamePlayer.gameObject.SetActive(true);
    37.     }
    38.  
    39.     public void AddGems(int numberOfGems)
    40.     {
    41.         gems += numberOfGems;
    42.         GemText.text = "Gems:" + gems;
    43.     }
    44. }

    and last but not least the GameMaster Script:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class gameMaster : MonoBehaviour
    7. {
    8.  
    9.  
    10.     public Text pointsText;
    11.     public Text InputText;
    12.  
    13.  
    14. }

    however keep in mind this is my first time making a game, most of this code was made when i'm listening to the tutorials... but i think they're all goods i guess?
     
  7. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,776
    Can you show also hierarchy tree?
    Specially parts, where Level Manager is attached to Game Objects.
    I assume, you got multiple scenes right?
     
  8. seankellygaller5

    seankellygaller5

    Joined:
    Jul 13, 2019
    Posts:
    16
    sure here:

    first level:

    upload_2019-7-22_22-20-45.png

    second Level:
    upload_2019-7-22_22-22-3.png

    third level:

    upload_2019-7-22_22-22-40.png

    is there anymore to add?
     
  9. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,776
    Does your gems holds actually any Tags?

    I would check, if as per line 24 in GemScript
    Code (CSharp):
    1. if(col.tag == "Player")
    your player got relevant tag Player in each scene.

    Add more Debug.Log () where appropriate, to track what is working and what is not.

    Check if in GemScript line 28
    Code (CSharp):
    1. Debug.Log("Score ");
    Is called (Level 1 (yes), 2 and 3 (no?) )

    Does all gems got colliders?
     
  10. seankellygaller5

    seankellygaller5

    Joined:
    Jul 13, 2019
    Posts:
    16

    yes they all have 2D colliders: they're all in triggered mode so when the player collides it, it triggeres and gets collected.
     
  11. seankellygaller5

    seankellygaller5

    Joined:
    Jul 13, 2019
    Posts:
    16
    um hello?
     
  12. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,487
    If you are able to package up your project and send me a link to it I could take a quick look. It'd probably be quicker asking lots of questions.

    In the end though, it's pretty trivial to set a breakpoint in a script such as the collision callback or even add a Debug.Log statement in various places to see what's not working so maybe do that to narrow down what's going wrong.
     
  13. seankellygaller5

    seankellygaller5

    Joined:
    Jul 13, 2019
    Posts:
    16
    ok but how do i package my project to you?
     
  14. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,487
    Up to you but maybe zip it and place it on an online service for me to upload such as Google drive etc.
     
  15. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,487
    You are free to upload it here.

    NOTE: Please delete the library/obj/logs folders before you zip it to keep the size down too.
     
  16. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,487
    So looking at your project, your player is a prefab but you have an instance of it in each level. Your collection of gems is based upon a script on each gem that checks if the collider object has a "player" tag. In the first it does but in the remaining levels it is untagged so doesn't work.

    Changing the tag on them to "player" starts gem collection in those levels.
     
    Last edited: Jul 23, 2019
    seankellygaller5 and Antypodish like this.
  17. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,776
    Something I did sensed :)
     
    seankellygaller5 likes this.
  18. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,487
    Yes, in this case the gems are untagged and the trigger script is on the gems which checks for a tagged player object. Same problem, different direction.
     
    Antypodish and seankellygaller5 like this.