Forum begins after the advertisement:


[Part 2] SerializedObjectNotCreatableException: Object at index 0 is null

Home Forums Video Game Tutorial Series Creating a Rogue-like Shoot-em Up in Unity [Part 2] SerializedObjectNotCreatableException: Object at index 0 is null

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #17249
    Piskorskee skee
    Level 2
    Participant
    Helpful?
    Up
    0
    ::

    Hi everyone!

    So I have been watching this tutorial: Creating a Rogue-like (like Vampire Survivors) in Unity — Part 2: Map Generation

    And about 25th minute I ran on this bug:

    SerializedObjectNotCreatableException: Object at index 0 is null

    Below both scripts that I changed and since that time I have this bug (I can still run the game though):

    MapController:

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    public class MapController : MonoBehaviour
    {
    public List<GameObject> terrainChunks;
    public GameObject player;
    public float checkerRadius;
    Vector3 noTerrainPosition;
    public LayerMask terrainMask;
    public GameObject currentChunk;
    PlayerMovement pm;
    
    // Start is called once before the first execution of Update after the MonoBehaviour is created
    void Start()
    {
    pm = FindFirstObjectByType<PlayerMovement>();
    }
    
    // Update is called once per frame
    void Update()
    {
    ChunkChecker();
    }
    
    void ChunkChecker()
    {
    if (!currentChunk)
    {
    return;
    }
    
    if (pm.moveDir.x > 0 && pm.moveDir.y == 0)
    {
    if (!Physics2D.OverlapCircle(currentChunk.transform.Find("Right").position, checkerRadius, terrainMask))
    {
    noTerrainPosition = currentChunk.transform.Find("Right").position;
    SpawnChunk();
    }
    }
    else if (pm.moveDir.x < 0 && pm.moveDir.y == 0)
    {
    if (!Physics2D.OverlapCircle(currentChunk.transform.Find("Left").position, checkerRadius, terrainMask))
    {
    noTerrainPosition = currentChunk.transform.Find("Left").position;
    SpawnChunk();
    }
    }
    else if (pm.moveDir.x == 0 && pm.moveDir.y > 0)
    {
    if (!Physics2D.OverlapCircle(currentChunk.transform.Find("Up").position, checkerRadius, terrainMask))
    {
    noTerrainPosition = currentChunk.transform.Find("Up").position;
    SpawnChunk();
    }
    }
    else if (pm.moveDir.x == 0 && pm.moveDir.y < 0)
    {
    if (!Physics2D.OverlapCircle(currentChunk.transform.Find("Down").position, checkerRadius, terrainMask))
    {
    noTerrainPosition = currentChunk.transform.Find("Down").position;
    SpawnChunk();
    }
    }
    else if (pm.moveDir.x > 0 && pm.moveDir.y > 0)
    {
    if (!Physics2D.OverlapCircle(currentChunk.transform.Find("Right Up").position, checkerRadius, terrainMask))
    {
    noTerrainPosition = currentChunk.transform.Find("Right Up").position;
    SpawnChunk();
    }
    }
    else if (pm.moveDir.x > 0 && pm.moveDir.y < 0)
    {
    if (!Physics2D.OverlapCircle(currentChunk.transform.Find("Right Down").position, checkerRadius, terrainMask))
    {
    noTerrainPosition = currentChunk.transform.Find("Right Down").position;
    SpawnChunk();
    }
    }
    else if (pm.moveDir.x < 0 && pm.moveDir.y > 0)
    {
    if (!Physics2D.OverlapCircle(currentChunk.transform.Find("Left Up").position, checkerRadius, terrainMask))
    {
    noTerrainPosition = currentChunk.transform.Find("Left Up").position;
    SpawnChunk();
    }
    }
    else if (pm.moveDir.x < 0 && pm.moveDir.y < 0)
    {
    if (!Physics2D.OverlapCircle(currentChunk.transform.Find("Left Down").position, checkerRadius, terrainMask))
    {
    noTerrainPosition = currentChunk.transform.Find("Left Down").position;
    SpawnChunk();
    }
    }
    }
    void SpawnChunk()
    {
    int rand = Random.Range(0, terrainChunks.Count);
    Instantiate(terrainChunks[rand], noTerrainPosition, Quaternion.identity);
    }
    }
    

    ChunkTrigger:

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    public class ChunkTrigger : MonoBehaviour
    {
    MapController mc;
    
    public GameObject targetMap;
    
    void Start()
    {
    mc = FindFirstObjectByType<MapController>();
    }
    
    private void OnTriggerStay2D(Collider2D col)
    {
    if (col.CompareTag("Player"))
    {
    mc.currentChunk = targetMap;
    }
    }
    
    private void OnTriggerExit2D(Collider2D col)
    {
    if (col.CompareTag("Player"))
    {
    if (mc.currentChunk == targetMap)
    {
    mc.currentChunk = null;
    }
    }
    }
    }
    

    I have had to use FindFirstObjectByType as I’m using Unity 6

    Would you be able to help me spot the bug or do you need any other information? Thanks!

    #17250
    Piskorskee skee
    Level 2
    Participant
    Helpful?
    Up
    1
    ::

    Okay nvm, I just restarted Unity and no bugs anymore :P

    has upvoted this post.
    #17251
    Terence
    Level 30
    Keymaster
    Helpful?
    Up
    0
    ::

    @piskorskeeskee glad you managed to fix your issue. Any errors with serialized objects are more likely than not issues with the editor that can be fixed by closing and reopening.

Viewing 3 posts - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.

Go to Login Page →


Advertisement below: