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.
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?
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.
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?
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.
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.
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.