Problems with my Prefab

Hi,
First of all, i’m not english so excuse me if i’m not totally understandable.

My problem is a NullReferenceException :

NullReferenceException
UnityEngine.Object.Internal_InstantiateSingle (UnityEngine.Object data, Vector3 pos, Quaternion rot) (at C:/BuildAgent/work/ea95e74f6e5f192d/Runtime/ExportGenerated/Editor/UnityEngineObject.cs:72)
UnityEngine.Object.Instantiate (UnityEngine.Object original, Vector3 position, Quaternion rotation) (at C:/BuildAgent/work/ea95e74f6e5f192d/Runtime/ExportGenerated/Editor/UnityEngineObject.cs:82)
Aera.launch (Int32 x, Int32 y, Int32 z) (at Assets/New Folder/Aera.cs:23)
BattlefieldBase.launch (Int32 i) (at Assets/New Folder/BattlefieldBase.cs:22)
ButtonPlay.OnGUI () (at Assets/ButtonPlay.cs:18)

My part of code is :

using UnityEngine;
using System.Collections;

public class Aera : ScriptableObject
{
private Object aeraModel;
private Vector3 p;
private Quaternion r;
void OnEnable()
{
aeraModel = GameObject.Find(“aeraPrefab”);
Debug.Log(“GOOD”);
}
public void launch (int x, int y, int z)
{
if (aeraModel == null)
{
Debug.Log(“snif”);
}
p = new Vector3((float)BattlefieldBase.getSize()* x, (float)BattlefieldBase.getSize()* y, (float)BattlefieldBase.getSize()* z);
r = Quaternion.Euler(0,0,0);
Instantiate(aeraModel, p, r);
}
}

I spotted the problem with Log : My prefab don’t “load”, console print “snif”.
Explain me why … i checked the spelling mistake in the prefab name. There is no one.

I read a lot of topics on this forum, but no one help me.

Previously thanks
Pierre

2 Answers

2

GameObject.Find is used to find an object which already exists in the scene, not a prefab which hasn’t been instantiated.

You can either have aeraModel as a Transform and drag the prefab into the aeroModel slot in the inspector, or you can save your prefab in the Resources folder and instantiate it from there.

Option A (I recommend):

    public Transform aeraModel;
    private Vector3 p;
    private Quaternion r;

    public void launch(int x, int y, int z) {
        p = new Vector3((float)BattlefieldBase.getSize() * x, (float)BattlefieldBase.getSize() * y, (float)BattlefieldBase.getSize() * z);
        r = Quaternion.Euler(0, 0, 0);
        Instantiate(aeraModel, p, r);
    }

Option B (put prefab in a folder named Resources):

private Transform aeraModel;
private Vector3 p;
private Quaternion r;

void OnEnable() {
    aeraModel = Resources.Load("aeraPrefab") as Transform;
}

public void launch(int x, int y, int z) {
    p = new Vector3((float)BattlefieldBase.getSize() * x, (float)BattlefieldBase.getSize() * y, (float)BattlefieldBase.getSize() * z);
    r = Quaternion.Euler(0, 0, 0);
    Instantiate(aeraModel, p, r);
}

Thanks for this fast answer. In fact, my problem was method "find", i will take the first solution :) Sorry, i forgot the 101 button (i coding all night ^^) Again thanks for answers. Pierre

Please do not post comments as answers. I have converted this answer to a comment. In the future please use the Add new comment button. :)

First suggestion is when you use this fourm you should always put your code in the code text. To do that either click the 101 button and copy and paste your code there. or highlight your code and press the button.

    using UnityEngine; using System.Collections;
   
 
   public class Aera : ScriptableObject 
   { 
      private Object aeraModel; 
      private Vector3 p;
      private Quaternion r; 
      void OnEnable() 
      { 
           aeraModel = GameObject.Find("aeraPrefab"); 
           Debug.Log("GOOD"); 
      } 
      public void launch (int x, int y, int z) 
      { 
           if (aeraModel == null) 
           { 
                Debug.Log("snif"); 
           } 
           p = new Vector3((float)BattlefieldBase.getSize()* x,(float)BattlefieldBase.getSize()* y, (float)BattlefieldBase.getSize()* z); 
           r = Quaternion.Euler(0,0,0); Instantiate(aeraModel, p, r); 
       } 
   }

As far as your problem im don’t know c# but I think your code looks right. Make sure that your prefab name doesn’t have a space after it. I had that problem before. if not then does your object exist when you try to find it? Or is it created afterwards?