00:00
00:00
Newgrounds Background Image Theme

mcd410 just joined the crew!

We need you on the team, too.

Support Newgrounds and get tons of perks for just $2.99!

Create a Free Account and then..

Become a Supporter!

Need some help with a C# Project

1,853 Views | 4 Replies
New Topic Respond to this Topic

Need some help with a C# Project 2014-10-28 09:49:46


So I am currently developing a small 2D platformer! However I am having some issues with my Checkpoint system, I am still very new to C# and Unity so don't be to harsh on me!

Anyways, what is ment to happen is, Player touches checkpoint (a blue gem) The blue gem dissapears and makes a noise and then that is where he will spawn!

The issue is, the checkpoints work fine, but if I add the whole "Make a noise and then dissapear" It will make the noise and dissapear but not set his checkpoint anymore! >_<

Here is the code, but I would really prefer someone helping me over teamviewer or something, so I can learn how to fix the issue!

using UnityEngine;
using System.Collections;

public class CheckPoint : MonoBehaviour {
	
	public static bool checkpointEnabled = false;
	
	public static GameObject initSpawn;
	public static GameObject checkpoint;
	public static GameObject respawn;
	public AudioClip[] audioClip;
	
	void Start ()
	{
		initSpawn = GameObject.FindWithTag("Spawn");
		checkpoint = GameObject.FindWithTag("CheckPoint");
		respawn = initSpawn;
	}
	
	void Update ()
	{
		if (checkpointEnabled)
		{
			respawn = checkpoint;
		}
		

	}


	//Set the checkpoint!
	void OnTriggerEnter(Collider col)
	{
		if (col.gameObject.tag == "Player")
		{
			checkpointEnabled = true;
			Debug.Log ("CheckPoint!");
		}
	}


	//Play sound and delete object
	void OnTriggerEnter2D(Collider2D col)
	{
		if (col.gameObject.tag == "Player") 
		{
			transform.Translate(Vector3.up * 15f, Space.World);
			PlaySound (0);
			Destroy(this.gameObject, 1f);
		}
		
	}


	void PlaySound(int clip)
	{
		audio.clip = audioClip [clip];
		audio.Play ();
	}

}

Response to Need some help with a C# Project 2014-10-29 14:50:54


This may or may not work, but it's a method I would try. I apologize for the different method of typing this out, force of habit hopping between VB and C#.

using UnityEngine;
using System.Collections;

public class CheckPoint : MonoBehaviour {
	
	public static bool checkpointEnabled = false;
	
	public static GameObject initSpawn;
	public static GameObject checkpoint;
	public static GameObject respawn;
	public AudioClip[] audioClip;
	public bool initSafe = false;
	
	void Start ()
	{
		initSpawn = GameObject.FindWithTag("Spawn");
		checkpoint = GameObject.FindWithTag("CheckPoint");
		respawn = initSpawn;
		
		if (initSafe == true)
		{
			PlaySound (0);
		}
		
	}
	
	void Update ()
	{
		if (checkpointEnabled)
		{
			respawn = checkpoint;
		}
		

	}


	//Set the checkpoint!
	void OnTriggerEnter(Collider col)
	{
		if (col.gameObject.tag == "Player")
		{
			checkpointEnabled = true;
			Debug.Log ("CheckPoint!");
		}
	}


	//Play sound and delete object
	void OnTriggerEnter2D(Collider2D col)
	{
		if (col.gameObject.tag == "Player") 
		{
			transform.Translate(Vector3.up * 15f, Space.World);
			Destroy(this.gameObject, 1f);
			initSafe = true;
		}
		
	}


	void PlaySound(int clip)
	{
		audio.clip = audioClip [clip];
		audio.Play ();
	}

}

Response to Need some help with a C# Project 2014-11-10 18:34:27


Is there a reason you're checking for a collision with the player in both OnTriggerEnter and OnTriggerEnter2D? If the player only has a 2D collider that would explain why only the code in OnTriggerEnter2D is being called.

Response to Need some help with a C# Project 2014-11-12 02:36:26


At 11/10/14 06:34 PM, onarru wrote: Is there a reason you're checking for a collision with the player in both OnTriggerEnter and OnTriggerEnter2D? If the player only has a 2D collider that would explain why only the code in OnTriggerEnter2D is being called.

Yeah there is! Using OnTriggerEnter the checkpoint system works, but the sound does not play, and the item does not vanish!
Using OnTriggerEnter2D The items will vanish and sound will play, but the checkpoint itself however does not work.. So I am a bit confused as to why this is happening!

Response to Need some help with a C# Project 2014-11-13 13:02:30


Without seeing your project its hard to say what might be causing the problem but I do have a few questions that might clarify the problem or lead you to another solution. Keep in mind while I have a good bit of experience with Unity I'm not an expert so take anything I say with a grain of salt.

Is this script attached to the checkpoint object? The script searches for an object tagged "Checkpoint" so I assume this script is not attached to the checkpoint object (or else it could grab the GameObject using this.gameObject).

For respawning you don't need to store the GameObject, you could just store the transform.position of the checkpoint and when the player needs to respawn instantiate them at that position. I would also store the respawn location/object in whatever script is responsible for the actual respawning (probably the player's controller) rather than statically in the Checkpoint class. If your player object is destroyed when he dies and that's why you aren't storing the respawn location there you can create a separate object responsible for respawning the player or causing a game over when he dies and send the respawn location to it when it changes.

You shouldn't assign the respawn location every update frame, just once when it changes.

Occasionally sounds stop playing if their source is destroyed too quickly, I assume this is why you're delaying the Destroy() call, you could instead look into AudioSource.PlayClipAtPoint() which creates a new audiosource to play your clip and destroys it when it's done. This way it doesn't matter if the checkpoint is destroyed during playback. It's not terribly efficient, but in your game it's unlikely to be a performance problem.

Those might not solve your problem but they're some good tips to follow for a beginner. If you want I'd be happy to look at your project if you want to mail me a zip. It's hard to say what the problem is without more information, but there's no reason you should need to use 2d and 3d physics in your game and I suspect that might have something to do with it. Just PM me if you want me to take a look at it or post some more of the code for your player or some screenshots of the components on the player and the checkpoint.