- Home /
Why is my Prefab Instantiating when the Scene is Loaded?
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
}
}
}
}
Answer by ScaniX · Jul 10, 2016 at 03:34 PM
If I understand correctly, you get your "prefab" from the scene with this line:
harpoon_00001 = GameObject.Find("harpoon_00001");
GameObject.Find() only finds objects in the scene that are active, so I think the moon lady is correct when she says that you have that object in the editor scene.
If you want to have the prototype harpoon in the scene (in contrast to store the harpoon as a "real" prefab and drag it onto your member variable or use Resource.Load()), then set it as inactive and assign it to your script using the inspector by making the member variable public.
public GameObject harpoon_00001;
@ScaniX- The harpoon does stop instantiating at the beginning when I delete it from then scene, but then another problem comes up. The harpoons can't destroy the fish if there isn't an instance of the harpoon on the scene, for some reason. Any ideas on how to fix that?
Not really. If you did the following:
drag your scene harpoon to the prefab folder to store that instance with its scripts and other components
delete the instance in the scene
drag the prefab from the project explorer to your prefab field in the inspector
It should work. The harpoon should be instantiated from the prefab with all its scripts and work like before.
I just did what you said, but the harpoon still doesn't kill the fish. Ins$$anonymous$$d, the console says this-
NullReferenceException: Object reference not set to an instance of an object harpoonScript.OnTriggerEnter2D (UnityEngine.Collider2D other) (at Assets/Levels/Scripts/harpoonScript.cs:30)
@ScaniX I put in the code from your newest comment, commented out the part about the fish label, and it works now! Thanks for your help!
Answer by AnneSchmidt_legacy · Jul 10, 2016 at 02:02 PM
Are you sure you didn't leave an instance of the harpoon on the scene?
Your answer
Follow this Question
Related Questions
Instantied 2D Prefab Is Invisible 1 Answer
How to move Instantiated 2D objects by 0.5 using arrows 1 Answer
How to make TCG Deck (Instatiate based on Prefab) 1 Answer
Destroying assets is not permitted to avoid data loss. 0 Answers
How do I make a clone of a prefab appear on the correct layer? [5.2.2f1] 1 Answer