Hello! I am creating a 2D video game where a player can shoot a harpoon when the space bar is pressed, and can move left, right, up, and down using the arrow keys.
When I start my game, the prefab of the harpoon automatically instantiates, although the space bar has not been pressed yet.
Is there something wrong with my code? Do I need to change something in the inspector? What are some other reasons that the prefab instantiates on start? Thanks in advance- George
Here is my code-
harpoonScript.cs
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class harpoonScript : MonoBehaviour {
// Public variable
public int speed = 6;
private Rigidbody2D r2d;
public Text FishLabel;
public int fishKillCount = 0;
// Function called once when the bullet is created
void Start () {
// Get the rigidbody component
r2d = GetComponent<Rigidbody2D>();
// Make the bullet move upward
float ySpeed = 0;
float xSpeed = -8;
Vector2 newVelocity = new Vector2(xSpeed, ySpeed);
r2d.velocity = newVelocity;
}
void OnTriggerEnter2D(Collider2D other) //hero hits side of enemy
{
if (r2d = null) {
Debug.Log("Unable to find GameObject, reference is null");
} else {
string fishStringfromText = FishLabel.text;
int fishIntfromText;
fishIntfromText = int.Parse(fishStringfromText);
++fishIntfromText;
FishLabel.text = fishIntfromText.ToString();
Debug.Log("GameObject found, reference is not null");
Destroy(other.gameObject.GetComponent<Collider2D>()); //Remove collider to avoid audio replaying
other.gameObject.GetComponent<Renderer>().enabled = false; //Make object invisible
Destroy(other.gameObject, 0.626f); //Destroy object when audio is done playing, destroying it before will cause the audio to stop
Destroy(GameObject.Find("harpoon_00001(Clone)"));
}
}
}
playerMove.cs
using UnityEngine;
using System.Collections;
public class playerMove : MonoBehaviour {
// All Variables
public float speed = 10;
private Rigidbody2D rigidBody2D;
private GameObject harpoon_00001;
private bool facingRight = true;
void Awake () {
rigidBody2D = GetComponent<Rigidbody2D>();
harpoon_00001 = GameObject.Find("harpoon_00001");
if (harpoon_00001 == null) {
Debug.Log("Unable to find GameObject, reference is null");
} else {
Debug.Log("GameObject found, reference is not null");
}
}
void Update () {
if (Input.GetKeyDown(KeyCode.LeftArrow) && !facingRight) {
Flip();
}
if (Input.GetKeyDown(KeyCode.RightArrow) && facingRight) {
Flip();
}
}
void Flip () {
facingRight = !facingRight;
Vector3 theScale = transform.localScale;
theScale.x *= -1;
transform.localScale = theScale;
}
void FixedUpdate () {
float xMove = Input.GetAxis("Horizontal");
float yMove = Input.GetAxis("Vertical");
float xSpeed = xMove * speed;
float ySpeed = yMove * speed;
Vector2 newVelocity = new Vector2(xSpeed, ySpeed);
rigidBody2D.velocity = newVelocity;
if (Input.GetKeyDown("space")) {
GetComponent<AudioSource>().Play();
harpoonScript harpoon = Instantiate(harpoon_00001,transform.position, Quaternion.identity) as harpoonScript;
// Assuming harpoon prefab already facing to right
if (facingRight) {
harpoon.transform.eulerAngles = new Vector3(0f, 0f, 180f); // Face backward
Vector3 theScale = harpoon.transform.localScale;
theScale.x *= -1;
harpoon.transform.localScale = theScale; // Flip on y axis
}
}
}
}