Search Unity

Need Help for Prefab Spawner

Discussion in 'Scripting' started by FernandoAlonso, Sep 29, 2017.

  1. FernandoAlonso

    FernandoAlonso

    Joined:
    Sep 12, 2017
    Posts:
    34
    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;

    [/url']https://i.hizliresim.com/pGoWBL.png[/img][/url]

    And this is the code that i am using.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. public class CylinderManager : MonoBehaviour
    5. {
    6.      public GameObject[] cylinderPrefabs;
    7.      private Transform playerTransform;
    8.      private float spawnZ = 0.0f;
    9.      private float cylinderLength = 30.0f;
    10.      private int amnCylindersOnScreen = 7;
    11.      // Use this for initialization
    12.      private void Start () {
    13.          playerTransform = GameObject.FindGameObjectWithTag ("Player").transform;
    14.          for (int i = 0; i < amnCylindersOnScreen; i++)
    15.          {
    16.              SpawnTile ();
    17.          }
    18.      }
    19.    
    20.      // Update is called once per frame
    21.      private void Update () {
    22.          if (playerTransform.position.z > (spawnZ - amnCylindersOnScreen * cylinderLength))
    23.          {
    24.              SpawnTile ();
    25.          }
    26.        
    27.      }
    28.      private void SpawnTile(int prefabIndex = -1)
    29.      {
    30.          GameObject go;
    31.          go = Instantiate (cylinderPrefabs [0]) as GameObject;
    32.          go.transform.SetParent (transform);
    33.          go.transform.position = Vector3.forward * spawnZ;
    34.          spawnZ += cylinderLength;
    35.      }
    36. }
     
  2. larswik

    larswik

    Joined:
    Dec 20, 2012
    Posts:
    312
    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?
     
  3. larswik

    larswik

    Joined:
    Dec 20, 2012
    Posts:
    312
    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.
     
  4. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    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). :)
    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 :)
    You can send it a value there, to replace that option, or just leave it..
     
    larswik likes this.
  5. FernandoAlonso

    FernandoAlonso

    Joined:
    Sep 12, 2017
    Posts:
    34
    So you have a suggestion to solve that?
     
  6. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    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 :)

    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 :) :)
     
  7. FernandoAlonso

    FernandoAlonso

    Joined:
    Sep 12, 2017
    Posts:
    34
    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.
     
  8. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    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?
     
  9. FernandoAlonso

    FernandoAlonso

    Joined:
    Sep 12, 2017
    Posts:
    34
    You can see the cylinder prefabs on the inspector panel.Here is the ss of it;



     
  10. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    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 " { "
    :)
     
  11. FernandoAlonso

    FernandoAlonso

    Joined:
    Sep 12, 2017
    Posts:
    34
    Instantiate is on the line 39

    Line 29;

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

    Sorry for being inexperience :)
     
  12. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    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 :) :)

    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.. :)
    From there you should be able to start getting it all together..
     
  13. FernandoAlonso

    FernandoAlonso

    Joined:
    Sep 12, 2017
    Posts:
    34
    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 :)
     
    Last edited: Sep 30, 2017
  14. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    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 :)
    Glad you got it resolved. :)