Lesson 2.2 Destroy projectiles

Currently completing the pathway for junior programmer on Unity but ran into this issue in Lesson 2.2:

ISSUES:

At step 6 we are instructed to create Create “DestroyOutOfBounds” script and apply it to the projectile. Once I attached the script to the, prefab and try to run it, it no longer recreates the pizza projectile. I feel like it is deleting the original game object and not the prefab. When I de-select the DestroyOutOfBounds code, I am able to shoot the pizza projectile.

Background info:
→ I made sure to link the Food_Pizza_01 prefab (from prefab folder) to the Player Projectile Prefab.
→ I made sure to link the DestroyOutofBounds + MoveForward Script to the Food_Pizza_01.; I made sure to override and apply all.
→ I HAVE FOLLOWED EVERY STEP IN THE TUTORIAL BUT IT DONT WORK

Here is screenshot of the code:

PlayerController.cs 
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerController : MonoBehaviour
{

    public float horizontalInput;
    public float speed = 10.0f; 
    public float xRange = 10.0f; 
    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 * horizontalInput * Time.deltaTime * speed);


        if (Input.GetKeyDown(KeyCode.Space))
        {

            // Launch Pizza 
             Instantiate(projectilePrefab, transform.position, projectilePrefab.transform.rotation);

        }


    }
}


MoveForward
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MoveForward : MonoBehaviour
{

public float speed = 30.0f;
// Start is called before the first frame update
void Start()
{
    
}

// Update is called once per frame
void Update()
{
 transform.Translate (Vector3.forward * Time.deltaTime * speed);

}

}

DestroyOutofBounds
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class DestroyOutofBounds : MonoBehaviour
{


    private float topBound = 40; 
    private float lowerBound = -10;
    // Start is called before the first frame update
    void Start()
    {
        
    }
    // Update is called once per frame
    void Update()
    {
        if (transform.position.z > topBound)
        {
            Destroy(gameObject);
            
        } if (transform.position.z < lowerBound){
           Destroy(gameObject);
        }
        {



        }

    }
}

Thank you Unity Community for any help you offer. I have tried to look at other discussions with no success.

When you dragged over the Food_Pizza Prefab into the Player Controller Script did you drag it from your Prefab folder or Main Game(Prototype 2). You want to have the one from the prefab folder in the Player Controller Script.
Hope this helps with your problem

Hi @EveryTitan44

Thank you very much for the response!

Yes I dragged the the Food_Pizza Prefab from the Prefab folder.

I dragged it from this folder on the Player Controller (Script).

Even if I delete the object from the hierarchy it still wont work :frowning:

Thank you for your advice!! I will try it again!

Is it when u shoot the pizza it appears once or does it just never appear in the game view. You can also try adding Debug.Logs in places to see when the scripts getting activated and tell if there there is any miss timing.
For Example to see when the pizza is getting destroyed:

    private float topBound = 40; 
    private float lowerBound = -10;
    // Start is called before the first frame update
    void Start()
    {
        
    }
    // Update is called once per frame
    void Update()
    {
        if (transform.position.z > topBound)
        {
            Debug.Log("Pizza Destroyed")
            Destroy(gameObject);
            
        } 
        if (transform.position.z < lowerBound){
           Debug.Log("Pizza Destroyed")
           Destroy(gameObject);
        }

Hope this helps with your problem

This is a screen shot if I don’t add the DestroyOutofBounds script to the Food_Sandwich_01 prefab

Thank you all again!

It looks like your player is currently at Z -10.5, so I think the pizza is spawning and instantly seeing it’s out of bounds. Try moving to 0,0?

2 Likes

@EveryTitan44

Yes! I am only able to shoot the Pizza/ now sandwich once and then no more.

I tried adding your example of adding Debug.Logs and it still didnt do anything. It looks like it deletes the original one but doesn’t let me spawn others. Check out the image. It looks like it only destroys the initial pizza but doesnt let me shoot more pizzas or sandos.

Prior to the adding the DestroyOutofBounds i was able to shoot unlimited pizza/sando prefabs but after I add the DestroyOutofBounds I cant shoot but the initial pizza.

@Jayfor2 did sday2 solution solve your problem?

@sday2

OMG! Thank you very much!!! you solved my issue! I didn’t even think about moving on player!
Here is what it looks like with the new space!

THANK YOU AGAIN!!!

2 Likes

@EveryTitan44

YESS!!! But i appreciate you for your help! This is was my first time posting here and I’m new to unity so I appreciate the support :smiley:

Wish you nothing but the best in the future!

1 Like

One helpful tip is that if you feel compelled to assert in capital letters that you did everything correctly and it still fails, you are not actually helping anything.

You are especially not helping yourself by doing this because if you had done everything correctly, then most likely things would be working. :slight_smile:

A far more useful framing is to enumerate ALL the steps you have done, methodically checking each step so you are actually confident that they were done correctly.

Debugging also can give you insight. By debugging you can find out exactly what your program is doing so you can fix it.

Use the above techniques to get the information you need in order to reason about what the problem is.

You can also use Debug.Log(...); statements to find out if any of your code is even running. Don’t assume it is.

Once you understand what the problem is, you may begin to reason about a solution to the problem.

Remember with Unity the code is only a tiny fraction of the problem space. Everything asset- and scene- wise must also be set up correctly to match the associated code and its assumptions.