Hello, i need help with one error in my game, 2D platform for android. I have class for moving objects(traps) which working correctly, then i have class which is called only when i touch my own layer (TeleportLayer) and when i touch this layer then my character is teleported to definet Vector3 position. I want to call function from movingobject class in teleport class, but when i make it, then i get NullReferenceException, don’t know why.
in moving object i have function
public void ResetPositions()
{
transform.position = defaultPosition;
}
and in teleport function i have this
.
.
.
private MoveObject moveObject;
void Start()
{
moveObject = GetComponent<MoveObject>();
textScore = GameObject.Find("ScoreNumber").GetComponent<Text>();
kamera = GameObject.Find("Kamera").transform;
}
void Update()
{
var pos = bottonPosition;
pos.x += transform.position.x;
pos.y += transform.position.y;
standing = Physics2D.OverlapCircle(pos, collisionRadius, collisionLayer);
if (standing)
{
transform.position = spawnLocation;
kamera.position = CameraRestartPoint;
moveObject.ResetPositions();
restartCount++;
textScore.text = restartCount.ToString();
}
}
How to fix it? Thanks a lot 
What does the full error say?
Full error is NullReferenceException
Object reference not set to an instance of an object and scriptname with line number and line with error is moveObject.ResetPositions(); (26)
So its likely that the GameObject that the script is attached to does not have a MoveObject script attached.
You could add one in the inspector or do it via script. Its also possible that maybe you are querying the wrong GameObject?
and can you please tell me how to correctly attach MoveObject script into another one?
what i need to do is call ResetPosition() from class CollisionStateTeleportToStart where i declare MoveObject and GetComponent and function ResetPosition() is declared in MoveObject C# class
This is what i have in IDE
This is Empty class where i have traps(quads) as childs Screenshot by Lightshot
And this is teleport script attached on player Screenshot by Lightshot
I found a problem, it’s been fixed via moveObject = GameObject.Find(“NameOfObject”).GetComponent();
2 Likes
One more question to solve completely this problem, i need to use GameObject.Find to main object and then find all childobjects and make foreach on them, how can i get all childs to array please?
Screenshot by Lightshot → i set GameObject.Find(“Traps”) and then i need to read all childs in this object.
Solved 
Solution:
private MoveObject[] moveObject;
void Start()
{
.
.
moveObject = GameObject.Find("Traps").GetComponentsInChildren<MoveObject>();
}
void Update()
{
.
.
foreach (MoveObject item in moveObject)
{
item.ResetPositions();
}
}
Thanks for your time and help how to find null object
THis “querying the wrong GameObject” help me to get solution, when i realize im calling object which i dont have attached to object and i need to call specific via name 
1 Like
-Also solved in this way: using the example above:
-instead of this, at line 5:
private MoveObject moveObject;
- use this:
private MoveObject moveObject = new MoveObject();