0

I am try to find the children of the transform that I insantiate. This is my code:

public Transform GetLevel(int _currentLevel)
{
    string levelName = "Level" + _currentLevel;
    Transform level2Load = MonoBehaviour.Instantiate(Resources.Load("Prefabs/Levels/" + levelName)) as Transform;
    Debug.Log(level2Load.childCount);
    return level2Load;
}

The problem is that I am getting the following error:

NullReferenceException: Object reference not set to an instance of an object LevelLoading.GetLevel (Int32 _currentLevel) (at Assets/Resources/Scripts/LevelScripts/LevelLoading.cs:10)

Does anybody know why?

edit*

The wierd thing is that it does find the transform and instantiates it. But it can't find the chrildren.

The children do appear in the scene, and if I attatch an script to the transform that looks for the children it finds them.

anonymous-dev
  • 2,897
  • 9
  • 48
  • 112
  • 3
    possible duplicate of [What is a NullReferenceException and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – maraaaaaaaa Aug 19 '15 at 18:19
  • which line is it referring to? – maraaaaaaaa Aug 19 '15 at 18:31
  • this line Debug.Log(level2Load.childCount); – anonymous-dev Aug 19 '15 at 18:34
  • 1
    does this work? `Transform level2Load = (Instantiate(Resources.Load("Prefabs/Levels/" + levelName))as GameObject).transform;` – maraaaaaaaa Aug 19 '15 at 18:38
  • Transform level2Load = (MonoBehaviour.Instantiate(Resources.Load("Prefabs/Levels/" + levelName)) as GameObject).transform; works! Could you explain why? – anonymous-dev Aug 19 '15 at 18:40
  • no idea, i thought actually the `MonoBehaviour` part was the problem, hence my answer – maraaaaaaaa Aug 19 '15 at 18:41
  • Sorry if you were planning to give my answer, @Andrew, I only saw your unedited "does this work?" comment without the code before I submitted it. Although I hope the explanation helps. – 31eee384 Aug 19 '15 at 18:49
  • this is honestly a weird problem.. does this work? `Transform level2Load = Instantiate(Resources.Load("Prefabs/Levels/" + levelName), typeof(Transform))as Transform;` – maraaaaaaaa Aug 19 '15 at 18:53

1 Answers1

2

The exception is from:

Debug.Log(level2Load.childCount);

level2Load is null. Therefore trying to access its childCount results in a null reference exception. This is probably due to the as in:

MonoBehaviour.Instantiate(Resources.Load("Prefabs/Levels/" + levelName)) as Transform;

Because as tries to typecast and returns null if the typecast is invalid, I think your issue is that the instantiated object is a GameObject, not a Transform.

Try casting to GameObject and using .transform instead:

GameObject level2Load = MonoBehaviour.Instantiate(Resources.Load("Prefabs/Levels/" + levelName)) as GameObject;
return level2load.transform;
31eee384
  • 2,748
  • 2
  • 18
  • 27
  • I don't understand why this would be the case though, since Unity states that you can do `Instantiate(...) as Texture2D` – maraaaaaaaa Aug 19 '15 at 18:51
  • I'm fairly sure the type of what you get from `Instantiate` depends on what the asset is. If you give it a path to a texture, you get a `Texture2D`, if you give it a path to a prefab, you get the root `GameObject`. `as` doesn't do *conversion*, just *typecasting*, if that makes sense. **Edit:** I noticed the `as` docs do refer to it as conversion between compatible types--so it does conversion, but only on the type level. There's no way for Unity to see that `as` and deliver different objects based on that. – 31eee384 Aug 19 '15 at 19:07