# NullReferenceException (635879)

**URL:** https://discussions.unity.com/t/nullreferenceexception-635879/635879
**Category:** Scripting
**Tags:** Scripting
**Created:** [August 9, 2016, 5:45am UTC](https://discussions.unity.com/t/nullreferenceexception-635879/635879 "2016-08-09T05:45:19Z")
**Posts on this page:** 11
**Page:** 1

<div class="post-metadata">

### Author: ![TwoTen](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/twoten/32/498699_2.png) [@TwoTen](https://discussions.unity.com/u/TwoTen)
#### Post date: [August 9, 2016, 5:45am UTC](https://discussions.unity.com/t/nullreferenceexception-635879/635879/1 "2016-08-09T05:45:19Z")

</div>

Hello!  
Hello. I have a wierd issue. This is the current code:

```csharp
    void Cmd_Spawn(float X, float Y, float Z)
    {

            Debug.Log(X + " | " + Y + " | " + Z);
            Transform spawn = null;
            spawn.position = new Vector3(X, Y, Z);
            spawn.rotation = new Quaternion(0, 0, 0, 0);
            GameObject player = (GameObject)Instantiate (playerPrefab, spawn.position, spawn.rotation);
            posComponent.TeleportPosition(spawn.position);
            posComponent.TeleportRotation(spawn.rotation);

        
    }

```

So the issue is that the line:  
spawn.position = new Vector3(X, Y, Z);  
For some reason it prints out an error. However. At the line above that wich actually debug.logs the correct values.  
Error message:  
NullReferenceException: Object reference not set to an instance of an object  
Spectator.Cmd\_Spawn (Single X, Single Y, Single Z) (at Assets/Scripts/Spectator.cs:55)

Any clues?

---

<div class="post-metadata">

### Author: ![jimroberts](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/jimroberts/32/494915_2.png) [@jimroberts](https://discussions.unity.com/u/jimroberts)
#### Post date: [August 9, 2016, 5:48am UTC](https://discussions.unity.com/t/nullreferenceexception-635879/635879/2 "2016-08-09T05:48:04Z")

</div>

You are trying to use a null transform reference “spawn” in pasted code. Assign “spawn” to the correct transform object to fix the error.

---

<div class="post-metadata">

### Author: ![TwoTen](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/twoten/32/498699_2.png) [@TwoTen](https://discussions.unity.com/u/TwoTen)
#### Post date: [August 9, 2016, 5:49am UTC](https://discussions.unity.com/t/nullreferenceexception-635879/635879/3 "2016-08-09T05:49:25Z")

</div>

> [@jimroberts](#):
>
> You are trying to use a null transform reference “spawn” on line 6, 7, and 8 of your pasted code.

But I did set the position and rotation? Doesnt that make it assinged? Or am i missunderstanding this?

---

<div class="post-metadata">

### Author: ![jimroberts](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/jimroberts/32/494915_2.png) [@jimroberts](https://discussions.unity.com/u/jimroberts)
#### Post date: [August 9, 2016, 5:51am UTC](https://discussions.unity.com/t/nullreferenceexception-635879/635879/4 "2016-08-09T05:51:31Z")

</div>

The Transform itself is null, you cannot use a null object because it doesn’t exist… Assigning position and rotation will not create a Transform.

---

<div class="post-metadata">

### Author: ![TwoTen](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/twoten/32/498699_2.png) [@TwoTen](https://discussions.unity.com/u/TwoTen)
#### Post date: [August 9, 2016, 5:52am UTC](https://discussions.unity.com/t/nullreferenceexception-635879/635879/5 "2016-08-09T05:52:08Z")

</div>

> [@jimroberts](#):
>
> The Transform itself is null, you cannot use a null object because it doesn’t exist… Assigning position and rotation will not create a Transform.

OH. So I would do transform spawn = new Transform?

---

<div class="post-metadata">

### Author: ![jimroberts](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/jimroberts/32/494915_2.png) [@jimroberts](https://discussions.unity.com/u/jimroberts)
#### Post date: [August 9, 2016, 5:57am UTC](https://discussions.unity.com/t/nullreferenceexception-635879/635879/6 "2016-08-09T05:57:13Z")

</div>

You should not use the “new” keyword to create Transforms. Create a public Transform field and assign an empty GameObject in your scene to it. Another option would be to skip the Transform altogether and just use a Vector3 for both the position and rotation.

---

<div class="post-metadata">

### Author: ![TwoTen](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/twoten/32/498699_2.png) [@TwoTen](https://discussions.unity.com/u/TwoTen)
#### Post date: [August 9, 2016, 5:58am UTC](https://discussions.unity.com/t/nullreferenceexception-635879/635879/7 "2016-08-09T05:58:25Z")

</div>

> [@jimroberts](#):
>
> You should not use the “new” keyword to create Transforms. Create a public Transform field and assign an empty GameObject in your scene to it. Another option would be to skip the Transform altogether and just use a Vector3 for both the position and rotation.

Thanks alot. Got it all working.

---

<div class="post-metadata">

### Author: ![jimroberts](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/jimroberts/32/494915_2.png) [@jimroberts](https://discussions.unity.com/u/jimroberts)
#### Post date: [August 9, 2016, 6:00am UTC](https://discussions.unity.com/t/nullreferenceexception-635879/635879/8 "2016-08-09T06:00:31Z")

</div>

> [@TwoTen](#):
>
> Thanks alot. Got it all working.

You could probably get away with just the following:

```auto
void Cmd_Spawn(float X, float Y, float Z)
{
   Debug.Log(X + " | " + Y + " | " + Z);
   GameObject player = Instantiate(playerPrefab, new Vector3(X, Y, Z), Quaternion.identity) as GameObject;
   posComponent.TeleportPosition(player.transform.position);
   posComponent.TeleportRotation(player.transform.rotation);
}

```

---

<div class="post-metadata">

### Author: ![TwoTen](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/twoten/32/498699_2.png) [@TwoTen](https://discussions.unity.com/u/TwoTen)
#### Post date: [August 9, 2016, 6:05am UTC](https://discussions.unity.com/t/nullreferenceexception-635879/635879/9 "2016-08-09T06:05:39Z")

</div>

> [@jimroberts](#):
>
> You could probably get away with just the following:
> 
> ```auto
> void Cmd_Spawn(float X, float Y, float Z)
> {
> Debug.Log(X + " | " + Y + " | " + Z);
> GameObject player = Instantiate(playerPrefab, new Vector3(X, Y, Z), Quaternion.identity) as GameObject;
> posComponent.TeleportPosition(player.transform.position);
> posComponent.TeleportRotation(player.transform.rotation);
> }
> 
> ```

With the code you provided I get a similar error:  
NullReferenceException: Object reference not set to an instance of an object  
Spectator.Cmd\_Spawn (Single X, Single Y, Single Z) (at Assets/Scripts/Spectator.cs:59)  
And line 59 is this:  
netComponent.TeleportPosition(player.transform.position);

What is that about?

---

<div class="post-metadata">

### Author: ![jimroberts](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/jimroberts/32/494915_2.png) [@jimroberts](https://discussions.unity.com/u/jimroberts)
#### Post date: [August 9, 2016, 6:09am UTC](https://discussions.unity.com/t/nullreferenceexception-635879/635879/10 "2016-08-09T06:09:35Z")

</div>

Either the player is failing to instantiate or “posComponent” is null.

---

<div class="post-metadata">

### Author: ![TwoTen](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/twoten/32/498699_2.png) [@TwoTen](https://discussions.unity.com/u/TwoTen)
#### Post date: [August 9, 2016, 6:11am UTC](https://discussions.unity.com/t/nullreferenceexception-635879/635879/11 "2016-08-09T06:11:05Z")

</div>

> [@jimroberts](#):
>
> Either the player is failing to instantiate or “posComponent” is null.

AH. Thats the key. Found my issue. Thanks alot.
