Search Unity

Bug Prefab Script is "null" but it exist

Discussion in 'Prefabs' started by JojoEffekt, Mar 22, 2022.

  1. JojoEffekt

    JojoEffekt

    Joined:
    Dec 21, 2020
    Posts:
    2
    Hey, I've tried literally everything, I'm now convinced it's a bug. But to begin with:
    I have a prefab with a script that I instantiate a few times. For each of these prefabs I query one and the same function from a differen script and gameobject: "string material = childToSave.GetComponent<ItemBlock>().material;"
    Yes the Script "ItemBlock" exist on every prefab. yes the script works, if i write in the start function Debug.Log it works for every created prefab. now my query works up to the 10th object, but after that it doesn't work anymore, NO MATTER what I try, it can only be a bug. After the 10th prefab it wrotes: "NullReferenceException: Object reference not set to an instance of an object" and the script shows that the "ItemBlock" == "null" but thats wrong. I dont know what to do, its surely a bug

    This is a chunk from my script that I want to access my prefabs from:

    public void SaveItems(){
    foreach(Transform child in rootObject.transform){
    GameObject childToSave = GameObject.Find(child.name);
    if(childToSave != null){
    //Debug.Log(childToSave.name);
    string material = childToSave.GetComponent<ItemBlock>().material; //get material to int
     
  2. JojoEffekt

    JojoEffekt

    Joined:
    Dec 21, 2020
    Posts:
    2
    okay it's really a S***ty bug, I have other prefab/gameobjects that are completely different and have their own parent. But because the names of the prefabs were the same, there were errors, but that shouldn't be, I defined exactly where the prefabs should be accessed in the functions. IT'S A BUG
     
  3. MirceaI

    MirceaI

    Unity Technologies

    Joined:
    Nov 24, 2020
    Posts:
    36