Search Unity

C# Game Problems

Discussion in 'Scripting' started by dodder312, Aug 24, 2015.

  1. dodder312

    dodder312

    Joined:
    Aug 24, 2015
    Posts:
    5
    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.
     
  2. gorbit99

    gorbit99

    Joined:
    Jul 14, 2015
    Posts:
    1,350
    Can you post your script?
     
  3. dodder312

    dodder312

    Joined:
    Aug 24, 2015
    Posts:
    5
    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.
     
  4. Nigey

    Nigey

    Joined:
    Sep 29, 2013
    Posts:
    1,129
    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):
    1.  ...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.
     
    bajeo88 likes this.
  5. bajeo88

    bajeo88

    Joined:
    Jul 4, 2012
    Posts:
    64
    When typing your reply click the 'Insert' -> 'Code'. Copy/paste your code inside the popup... It will look like this afterwards
     
    Nigey likes this.
  6. tedthebug

    tedthebug

    Joined:
    May 6, 2015
    Posts:
    2,570
    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.
     
    Nigey likes this.
  7. dodder312

    dodder312

    Joined:
    Aug 24, 2015
    Posts:
    5
    I put the code to destroy the player on the box collider of the guard.
     
  8. dodder312

    dodder312

    Joined:
    Aug 24, 2015
    Posts:
    5
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class GuardSight : MonoBehaviour {
    5.  
    6.     void OnTriggerEnter (Collider other) {
    7.             Destroy(other.gameObject);
    8.     }
    9. }
    That is the code I put on the guards.
     
  9. Korno

    Korno

    Joined:
    Oct 26, 2014
    Posts:
    518
    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):
    1.  void OnTriggerEnter(Collider other)
    2.         {
    3.             if (other.gameObject.name.Equals("Player"))
    4.             {
    5.                 Destroy(other.gameObject);
    6.             }
    7.         }
    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):
    1. public class GuardSight : MonoBehaviour
    2.     {
    3.         public GameObject ThePlayer;
    4.  
    5.         void OnTriggerEnter(Collider other)
    6.         {
    7.             if (ThePlayer != null && other.gameObject == ThePlayer)
    8.             {
    9.                 Destroy(other.gameObject);
    10.             }
    11.         }
    12.     }
     
  10. dodder312

    dodder312

    Joined:
    Aug 24, 2015
    Posts:
    5
    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.