0

A simple script doing a simple thing. Yet no matter what I cannot get it script to work.

using UnityEngine;
using System.Collections;

public class ShotScript : MonoBehaviour {
  public Player pScript;
  private Rigidbody2D shotRigid;

  public bool enemyContact = false;
  public bool enemyContactTrue = false;

  void Awake() {
    shotRigid = GetComponent<Rigidbody2D> ();
  }

  void Start() {
    shotRigid = GetComponent<Rigidbody2D> ();
  }

  void enemyContactFunction() {
    enemyContact = true;
  }

  void OnCollisionEnter2D (Collision2D col) {
    if (col.gameObject.tag == "Enemy1" ) {
      Debug.Log ("shot has collided with tagged enemy");
      if (enemyContact == false){
        enemyContactFunction();
      }   
    }
  } 
}

The debugs will go off but nothing ever happens in the game and this it taking me days to figure out. How would anyone else make something, anything happen, after a collision like this work when mine does not?

sheilak
  • 5,833
  • 7
  • 34
  • 43
Jason Q
  • 97
  • 1
  • 1
  • 11
  • the code looks fine to me, I just don't see it doing anything other than changing a variable form false to true. – Xander Luciano Dec 17 '15 at 22:12
  • that's exactly the problem, no matter how I re-write it, that damn variable will not change. I watch it in public and it never goes on. I have ran countless debugs and can never see it turning on. What else can I possibly do in my epic quest to change a singular variable??? – Jason Q Dec 17 '15 at 22:19
  • Ok well here is something new, if I just drag a prefab of the shot into the scene, and the enemies hit that, then the script runs fine. It must be something about how my shot is instantiating from player? Here is where that takes place: if (Input.GetKeyDown (KeyCode.R) && moveRight != false) { // nextFire = Time.time + fireRate; Instantiate(shot, shotSpawnRight.position, shotSpawnRight.rotation); – Jason Q Dec 17 '15 at 22:24
  • just wondering, are you trying to make an FPS of sorts? – Xander Luciano Dec 17 '15 at 23:20
  • no, just a top down game – Jason Q Dec 17 '15 at 23:21
  • ok, just keep in mind that if the velocity is high enough, there is possibilties of the shot "teleporting" through your character and never actually hitting anything. But back to the problem, can you run your game, shoot the object, and then pause the game and check if your instantiated object has the script attached to it – Xander Luciano Dec 17 '15 at 23:27
  • Add a breakpoint after Debug.Log and when it stops, see the value of enemyContact. Is it false? – RaidenF Dec 18 '15 at 15:19

0 Answers0