can you help me?

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.

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



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?

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.

  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?

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.

These layers.
4771958--454397--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.

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:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerMovement : MonoBehaviour
{

    public CharacterController2D controller;
    public Animator animator;
    public float runSpeed = 40f;

    float horizontalMove = 0f;
    bool Jump = false;
    bool Crouch = false;

    private gameMaster gm;
    public Vector3 respawnPoint;
    public LevelManager GameLevelManager;
    // Update is called once per frame

    void Start()
    {
        gm = GameObject.FindGameObjectWithTag("gamemaster").GetComponent<gameMaster>();
        Debug.Log("game master");
        respawnPoint = transform.position;
        GameLevelManager = FindObjectOfType<LevelManager>();
    }
    void Update()
    {

        horizontalMove = Input.GetAxisRaw("Horizontal") * runSpeed;

        animator.SetFloat("Speed", Mathf.Abs( horizontalMove));

        if (Input.GetButtonDown("Jump"))
        {
            Jump = true;
            animator.SetBool("IsJumping", true);
        }

        if (Input.GetButtonDown("Crouch"))
        {
            Crouch = true;
        } else if (Input.GetButtonUp("Crouch"))
        {
            Crouch = false;
        }
    }

    public void OnLanding()
    {
        animator.SetBool("IsJumping", false);
    }
   public void OnCrouching( bool isCrouching)
    {
        animator.SetBool("IsCrouching", isCrouching);
    }
    // Character Movement controls
    private void FixedUpdate()
    {
        controller.Move(horizontalMove * Time.fixedDeltaTime, Crouch, Jump);
        Jump = false;
    }


    void OnTriggerEnter2D(Collider2D col)
    {
       

        if(col.tag == "falldetector")
        {
            // what will happen if Player enters the fallDetector
            GameLevelManager.Respawn();
        }
       
    }
}

GemScript:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GemScript : MonoBehaviour
{

    private LevelManager gameLevelManager;
    public int gemValue;
    // Start is called before the first frame update
    void Start()
    {
        gameLevelManager = FindObjectOfType<LevelManager>();
    }

    // Update is called once per frame
    void Update()
    {
       
    }

    void OnTriggerEnter2D(Collider2D col)
    {
    if(col.tag == "Player")
        {
            gameLevelManager.AddGems(gemValue);
            Destroy(gameObject);
            Debug.Log("Score ");
        }
       

    }
}

LevelManager:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class LevelManager : MonoBehaviour
{
    public float respawnDelay;
    public PlayerMovement gamePlayer;
    public int gems;
    public Text GemText;
    // Start is called before the first frame update
    void Start()
    {
        gamePlayer = FindObjectOfType<PlayerMovement>();
        GemText.text = "Gems:" + gems;
    }

    // Update is called once per frame
    void Update()
    {
       
    }

    public void Respawn()
    {
        StartCoroutine("RespawnCoroutine");
      
    }

    public IEnumerator RespawnCoroutine()
    {
        gamePlayer.gameObject.SetActive(false);
        yield return new WaitForSeconds(respawnDelay);
        gamePlayer.transform.position = gamePlayer.respawnPoint;
        gamePlayer.gameObject.SetActive(true);
    }

    public void AddGems(int numberOfGems)
    {
        gems += numberOfGems;
        GemText.text = "Gems:" + gems;
    }
}

and last but not least the GameMaster Script:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class gameMaster : MonoBehaviour
{
  

    public Text pointsText;
    public Text InputText;

 
}

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?

Can you show also hierarchy tree?
Specially parts, where Level Manager is attached to Game Objects.
I assume, you got multiple scenes right?

sure here:

first level:

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

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

third level:

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

is there anymore to add?

Does your gems holds actually any Tags?

I would check, if as per line 24 in GemScript

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

Debug.Log("Score ");

Is called (Level 1 (yes), 2 and 3 (no?) )

Does all gems got colliders?

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

um hello?

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.

ok but how do i package my project to you?

Up to you but maybe zip it and place it on an online service for me to upload such as Google drive etc.

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.

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.

2 Likes

Something I did sensed :slight_smile:

1 Like

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.

2 Likes