C# function with multiple float arguments not working
I have a function that has three arguments that are all floats. It uses them to set the x, y, & z positions of the game object with this script, to that of another game object plus each float respectively
void moveTo(float xMove, float yMove, float zMove){
transform.position = new Vector3((GameObject.Find("Foxxy").transform.position.x + xMove), (GameObject.Find("Foxxy").transform.position.y + yMove), (GameObject.Find("Foxxy").transform.position.z + zMove));
}
It can be called using this, which says go to foxxy's position but 0.05 higher
moveTo(0f, 0.05f, 0f);
It's allowing me to run my game, but this part of the script can be clearly seen not functioning in the game view, as well as there being this error in the console:
NullReferenceException: Object reference not set to an instance of an object colliders2.moveTo (Single ext, Single why, Single zee) (at Assets/colliders.cs:16)
"colliders2" in this context is the name of the script in question that contains the moveTo function listed above. If anyone knows why this isn't working, any help is appreciated ;-;
$$anonymous$$y guess is Gameobject.Find() can't find a gameobject named "Foxxy", so it returns null.
Yep, that's exactly what happened >.> Thanks for figuring that out for me It seems that monodevelop decided to capitalize foxxy as an autocorrect, which it's been doing a lot of since I switched to C#
Answer by Tourist · Nov 14, 2017 at 09:05 AM
Try to keep the returned GameObject "Foxxy" in a variable before getting data from it. You would have found it is null and have gained performance in the process. GameObject.Find does cost a lot.
Your answer
Follow this Question
Related Questions
How to run a "foreach" through a list of voids 2 Answers
Floating Point Error? 1 Answer
Not finding a class in inspector (Easy) 0 Answers
What go inside the brackets in voids? 1 Answer
Not all code paths return a value error on a void function 0 Answers