Need Help for Prefab Spawner

Hello guys So i am pretty new on Unity and i found this tutorial

https://www.youtube.com/watch?v=HIsEqKPoJXM&list=PLLH3mUGkfFCXps_IYvtPcE9vcvqmGMpRK∈dex=7

and i tried to make the same process on my android game project but nothing happened.I couldnt figure out the problem.I will be so glad if you guys help me.Here is my map;

[[https://i.hizliresim.com/pGoWBL.png[/img][/url](https://i.hizliresim.com/XEoDz6.png[/img][/url)]

And this is the code that i am using.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CylinderManager : MonoBehaviour
{
     public GameObject[] cylinderPrefabs;
     private Transform playerTransform;
     private float spawnZ = 0.0f;
     private float cylinderLength = 30.0f;
     private int amnCylindersOnScreen = 7;
     // Use this for initialization
     private void Start () {
         playerTransform = GameObject.FindGameObjectWithTag ("Player").transform;
         for (int i = 0; i < amnCylindersOnScreen; i++)
         {
             SpawnTile ();
         }
     }
    
     // Update is called once per frame
     private void Update () {
         if (playerTransform.position.z > (spawnZ - amnCylindersOnScreen * cylinderLength))
         {
             SpawnTile ();
         }
        
     }
     private void SpawnTile(int prefabIndex = -1)
     {
         GameObject go;
         go = Instantiate (cylinderPrefabs [0]) as GameObject;
         go.transform.SetParent (transform);
         go.transform.position = Vector3.forward * spawnZ;
         spawnZ += cylinderLength;
     }
}

](pGoWBL.png)

That error message below left is basically saying that they object doesn’t exist. Null. My guess is you have not connected it. The script you posted is for the CylinderManager but in the photo you have the player selected so it is hard to see if the GameObject is attached to the code.

Also, it just seems odd to me but I tend to learn new code all the time but I have never seen before but the parameter in your SpawnTile method, prefabIndex you try to assign a -1 to it there which I have never seen anyone try to assign anything in that area of code before. But then in the body of the code, of that method you do not use the prefabIndex. Also in the different areas where you call the SpawnTire() you do not pass in any arguments.

I’m surprised that you didn’t get more compiling errors?)](https://hizliresim.com/pGoWBL)
[/quote]

I stand corrected, I looked at the tutorial and he does in deed write his code like you did and it works, I have just never seen it like that before. But your Null reference is more then likely due to you not connecting you prefab to the public field on the game side of the code.

the ‘-1’ is a default parameter. Those can be useful, when you think a method will use that parameter more often (at least, that’s my opinion on the matter). :slight_smile:
In this situation, it’s odd, because it’s never used… Maybe later it’s used in some updated code, I have no idea. Maybe it’s there because they thought they’d use it, but never got around to it :slight_smile:
You can send it a value there, to replace that option, or just leave it…

1 Like

So you have a suggestion to solve that?

Your screenshot was not helpful to me. sigh.

When you get a null ref error, it usually tells you on what line it happened. Then, you can use that to look into the issue, and at the very least, if you’re still stumped for some reason, post the line on the forums :slight_smile:

Anyhow, if the previous poster is correct, and you’re asking how to fix that… you drag your prefab into the public variable on the script …
If that’s the line that’s the problem… For example, if it’s the cylinderPrefabs that’s the problem, then just drag the prefab onto the script and it will automatically add it there.

If it’s something else, please just write down the line number as it corresponds to the code you posted on the forums :slight_smile: :slight_smile:

It says;

NullReferenceException: Object reference not set to an instance of an object
CylinderManager.Update () (at Assets/CylinderManager.cs:29)

i dragged cylinder manager onto the script but it didnt work.

Okay, I don’t think the line matches up exactly with what you posted here in the forums, but if it’s the instantiate call…
did you drag the prefab into the inspector where it says cylinderPrefabs?
Not drag the script, but the prefab in the project assets?

You can see the cylinder prefabs on the inspector panel.Here is the ss of it;

Okay, well then that’s all good… Is the line 29 equal to the instantiate call? You didn’t confirm that… just to rule anything out. In the code you posted, line 29 is only " { "
:slight_smile:

Instantiate is on the line 39

Line 29;

if (playerTransform.position.z > (spawnZ - amnCylindersOnScreen * cylinderLength))

Sorry for being inexperience :slight_smile:

Well, that explains a lot more… okay – see the error line you posted didn’t match the code you posted on the forums. When that happens, you should tell people what the line is on the forums, too :slight_smile: :slight_smile:

so the problem is almost certainly with ‘playerTransform’. Make sure you have a game object with that tag in the scene. In your start method, you can check if it’s null after you look for it… and confirm if that’s the issue, etc… :slight_smile:
From there you should be able to start getting it all together…

So i have a game object called Player on Scene but still keeps saying that error on console.

Hahaha.I figured out the problem.Player object was untagged that was the problem.I made its tag Player now it works.Thank you :slight_smile:

Yep, it was either not in the scene or not tagged, once I could see the correct line… it had to be one of those :slight_smile:
Glad you got it resolved. :slight_smile: