So I have some simple code, and now want to assign a new target position to move a sprite too (endxy).
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Thumper : MonoBehaviour
{
private Transform homexy;
private Transform endxy;
[SerializeField] private int speed;
[SerializeField] float endx;
[SerializeField] float endy;
private bool thump = true;
// Start is called before the first frame update
void Start ()
{
homexy.position = this.transform.position;
endxy.position = new Vector2 (endx, endy);
// also tried new Vector3 (endx, endy, 0f); // also didn't work
}
void Update ()
{
//other code to move Thumper here (removed for clarity)
}
}
However, The transforms (homexy & endxy) are always null - They do not populate with any values?
Can someone explain how I should set them up, and why the straight assignment doesnât work please?
Happy to try and work out the right code, when I understand why the above doesnât work.
Many Thanks (Be kind, first post here) 
I donât see any code above assigning values to either homexy or endxy and they are private non-serializefield, so⌠null!
The answer is always the same⌠ALWAYS!
How to fix a NullReferenceException error
https://forum.unity.com/threads/how-to-fix-a-nullreferenceexception-error.1230297/
Three steps to success:
- Identify what is null â any other action taken before this step is WASTED TIME
- Identify why it is null
- Fix that
ALSO: as will ALL Component-derived types you may never new() up one yourself. You must either drag it in or GetComponent() it from another GameObject.
Do these lines NOT do that then?
endx and endy DO have values.
- homexy.position = this.transform.position;
- endxy.position = new Vector2 (endx, endy);
homexy is a Transform, so youâd need to assign a Transform to it (which is a component attached to an object in the scene)
If you just want to store the position, then make them Vector2s
private Vector2 homexy;
private Vector2 endxy;
Correct. No more than buying sofas gives you a house to put them in.
Transforms, like any Unity Component, will NEVER exist anywhere except connected to a GameObject, so drag one in there from your scene or prefab or whatever youâre trying to do.
Otherwise if you just need to store a Vector3, then declare that type of field.
This might also help your brain lock around the idea of why that thing is null:
Value Types vs Reference Types:
https://discussions.unity.com/t/826396/4
Vector3s are value types (structs)
Transforms are reference types (classes)
Just for clarity, and to close this thread. I have since changed the Transforms to Vector3âs and they do the job perfectly.
I think I understand Kurtâs point about not being able to create a âTransformâ from thin air, but alter an existing one instead. I guess I made the ârookieâ mistake of thinking that a âTransformâ was just the Vector co-ordinates.
Anyhow - Thanks for the responses (especially flashframe), as that pointed me in the right direction, and my code is now working, and I am still learning 