Spawning a prefab

So I’m trying to spawn a random prefabs with a certain velocity. Basically a speedway. The code I wrote works however whenever the prefab is spawned it doesnt go forward like its supposed to

The settings of the prefab:
3107413--234813--upload_2017-6-14_13-46-58.png

Here is the code:

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

public class EnemySpawn : MonoBehaviour {

    public Transform[] spawnPoints; //spawnpoints
    public float speed = 30f;

    [System.Serializable]
    public class enemy
    {

        public string name;
        public GameObject prefab;
        public float chance; //the chance of it spawning
        public float delayTime; //a delay before they can start spawning

    }

    public enemy[] enemies;
    public float spawnDelay = 1.0f;

    enemy chosenEnemy;

    float random;

    float cumulative;
    public string selectedEnemy; //debug

    void Start()
    {

        InvokeRepeating("SpawnRandom", 1f, 1f);
    }

    void Update()
    {

    }

    void SpawnRandom()
    {
        //gets a random value
        random = Random.value;
        cumulative = 0f;

        for (int i = 0; i < enemies.Length; i++)
        {
            cumulative += enemies[i].chance;
            if (random < cumulative && Time.time >= enemies[i].delayTime)
            {
                selectedEnemy = enemies[i].name;
                chosenEnemy = enemies[i];
                Debug.Log("picked " + selectedEnemy);
                break;
            }
        }
        //the spawn code
        int spawnPointIndex = Random.Range(0, spawnPoints.Length);

Instantiate(chosenEnemy.prefab, spawnPoints[spawnPointIndex].position + transform.forward, spawnPoints[spawnPointIndex].rotation);
        chosenEnemy.prefab.GetComponent<Rigidbody>().AddForce(transform.forward * speed, ForceMode.Impulse);

    }
}

As it sits, you’re trying to move your prefab and not the object you just instantiated.

GameObject spawnedEnemy = Instantiate(chosenEnemy.prefab, spawnPoints[spawnPointIndex].position + transform.forward, spawnPoints[spawnPointIndex].rotation) as GameObject;
        spawnedEnemy.GetComponent<Rigidbody>().AddForce(transform.forward * speed, ForceMode.Impulse);

This will change your target to the recently spawned object. It may or may not move as I didn’t check that.

It doesn’t move and it gives me the:
“Object reference not set to an instance of an object
EnemySpawn.SpawnRandom () (at Assets/Scripts/EnemySpawn.cs:62)” error

Is it an idea to give the car/truck that spawn in a new script? that is on the prefab to make it move?

These errors mean that a value that Unity is expecting hasn’t been set. In this case, I’m guessing you haven’t loaded your enemies array with enemies prefabs in any way (ex. property inspector). You want to check the line in the error message (62… “EnemySpan.cs:62”) vs. that line in your code to figure out what’s erroring.

Your enemy should have a rigidbody on it when it spawns as well as the movement script that you showed in your picture, but if your Instantiate line is throwing an error, then either you have no spawn points or chosenEnemy.prefab isn’t returning anything. This is assuming line 62 still points to the Instantiate line.

What should it return? I’m new to C# and am slowly learning this.

Your enemy prefab.

Make sure your enemies array has something in it. I might even suggest at the start of your for loop to put:

Debug.Log("Enemy exists");

and check the console.

All that will do is print out “Enemy exists” for each enemy that is found in the enemies array. If you never see this message at all in the console, then you definitely haven’t added any enemy prefab(s) to the array.

EDIT: Actually, I see you already have a Debug.Log that should work nicely… do you ever see this in the console? If you don’t, then that definitely explains why you are getting the error… nothing is being chosen, which could be for more than one reason… one of which being, again, maybe you didn’t populate your enemies array.

To add enemies… you can do it in the property inspector (drag the prefab onto the enemies array of the script in the inspector), or programmatically.

If your enemies array is populated, than as Brath said, it’s probably that you’re missing a component that you’re referencing (such as rigid body) on one or more enemy prefabs that you’re using.

Actually…you could have an enemy object, but nothing assigned to the prefab variable.

Since your enemy class has a prefab variable that is a gameobject, if nothing is assigned to that variable, you’ll get that error you’re getting. So your EnemySpawn script has an array in the inspector. You set your array size and now you need to populate the values of each element to make sure you have your values correct. Mostly the prefab variable of the enemy as that is what instantiate is targeting.

Here is a tutorial I always refer to: unity spawn prefab tutorial has a little more detail on how you can spawn it and update your spawn points on the fly and how to prevent overlap of your objects and to spawn them on a grid.