I have been making a stealth game where if a collider of my player goes into another (and larger) collider of a guard it destroys the player but whenever I start the game the player just dissapears. I don't know what to do to fix it.
I would like to but as I am a new member to this I don't know how, I would be very grateful, however if you told me how.
Where you type, you'll see there's a list of options.. Like 'bold', 'italic'.. Things like that. There's one that is a picture of a text document. Click on that and it'll come up with a sub-menu, one saying 'code'. Select that. Simply paste your code into there. Once you hit the complete button the box'll disappear and your code appears in the text box with a Code (csharp): ...your code tags automatically appearing there. When you hit send, it'll automatically turn into code on the page. We'll need to see your code to help you here.
When typing your reply click the 'Insert' -> 'Code'. Copy/paste your code inside the popup... It will look like this afterwards
Are you checking what the player is colliding with? If you are only checking for a collision then destroying the player that could be the problem. You need to do a check on what it has collided with before destroying it.
Code (CSharp): using UnityEngine; using System.Collections; public class GuardSight : MonoBehaviour { void OnTriggerEnter (Collider other) { Destroy(other.gameObject); } } That is the code I put on the guards.
That will just destroy everything the guard collides with. Pretty scary guards! What you need to do is check that the other object you have collided with is the player. There are a few ways to do that. Idea 1 - Use the game object name. This is ok but if you change the player name in the future you will have to make sure to change the script. Not a great solution but you often see this in code in Unity. Code (CSharp): void OnTriggerEnter(Collider other) { if (other.gameObject.name.Equals("Player")) { Destroy(other.gameObject); } } Idea 2 - Use a public variable on the script. If you do this, you will notice a new property appear on the script component in the editor. Assign the ThePlayer value to the players game object (the game object that has the rigid body). This is better in most situations, but wont handle multiple players or if you dynamically create your player via script. But I recommend this way. Code (CSharp): public class GuardSight : MonoBehaviour { public GameObject ThePlayer; void OnTriggerEnter(Collider other) { if (ThePlayer != null && other.gameObject == ThePlayer) { Destroy(other.gameObject); } } }
Thank you all for helping me with this problem, it was a trivial matter really, I had just forgotten to put my player ON TOP of the plane so it just fell as it was half in and half out.