I’ve started the 2nd tutorial on create with code and got to the part where you learn to Destroy projectiles offscreen. I’ve followed the instructions but when I apply the DestroyOutOfBounds script to the food it won’t work as a prefab so I can’t use the food more than once, but if I remove the destroy script it works as a prefab. Can you tell me what I need to do please?
I’m not looking at the tutorial, but usually how instantiating from prefabs works if you have a reference to the original prefab in the Assets folder somewhere. You instantiate that prefab to add one of them to the scene at runtime. If you need another one, you instantiate from the original prefab again, not the instance of the prefab you previously instantiated. When you destroy one of them in the scene, make sure you aren’t destroying the original prefab, but instead the specific instance of it in the scene.
If you’re having the trouble you describe, I suspect you’re making one of two mistakes. Either you are trying to instantiate subsequent instances of the prefab off of the previous instance instead of the original prefab (which would give you null reference errors if you destroy the previous instance before instantiating another one), or when you are destroying you are destroying the original prefab in the Assets folder instead of the instance in the scene (which would again cause null reference errors whenever you try to instantiate another instance of the original prefab).
Generally you use the reference to the original prefab only to instantiate another one. When you instantiate it returns a reference to the new instance of it in the scene, so make sure you assign that to some other variable and not overwrite your reference to the original prefab in the Assets folder.
If you shared your code, and the specific error you are getting with the line numbers (I assume you’re getting a null reference error), it would be easier to see what is going wrong. Make sure you post code using CODE tags. See the sticky at the top of the Scripting forum.
I have looked in the console about the error “MissingReferenceException: The object of type “GameObject” has been destroyed but you are still trying to access it. Your script should either check if it is null or you should not destroy the object.” The sc ript it Takes me to if I click on it is this.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
public float horizontalInput;
public float speed = 15f;
public float xRange = 10f;
public GameObject projectilePrefab;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
if (transform.position.x < -xRange)
{
transform.position = new Vector3(-xRange, transform.position.y, transform.position.z);
}
if (transform.position.x > xRange)
{
transform.position = new Vector3(xRange, transform.position.y, transform.position.z);
}
horizontalInput = Input.GetAxis(“Horizontal”);
transform.Translate(Vector3.right * Time.deltaTime * speed * horizontalInput);
if (Input.GetKeyDown(KeyCode.Space))
{
Instantiate(projectilePrefab, transform.position, projectilePrefab.transform.rotation);
}
}
}
It highlights the bottom line
Instantiate(projectilePrefab, transform.position, projectilePrefab.transform.rotation);
I have checked and copied exactly what the tutorial tells you to put.
I have decided to restart the tutorial, but wondered if the problem could have been because I changed the projectilePrefab name to that from projectPrefab?
Assign your Projectile Prefab from the Prefab folder, not the Hierarchy. I had the same problem and this fixed the issue.