Im trying to reach SurfaceEditor2D from different objects. So i added script to objects which has SurfaceEditor.
Now i need help for two things;
1- Is there any other way to reach same components from different objects but do same job with them.
2- Why im getting NullReferenceExp Error here? Please help me.
Here is my PlayerController script;
bool isTouchingGround;
public bool IsTouchingGround()
{
return isTouchingGround;
}
public Sprite Speedy;
private Collider2D collider;
bool canMove = true;
void Start()
{
rb2d = GetComponent<Rigidbody2D>();
collider = GetComponent<Collider2D>();
surfaceEffector2D = GetComponent<SurfaceController>();
}
// Update is called once per frame
public void Update()
{
isTouchingGround = Physics2D.OverlapCircle(groundCheck.position,groundCheckRadius,groundLayer);
if(canMove)
{
RotatePlayer();
//RespondToBoost();
}
if(isTouchingGround)
{
/*Destroy(this.GetComponent<PolygonCollider2D>());
this.gameObject.GetComponent<SpriteRenderer>().sprite = Speedy;
this.gameObject.AddComponent<PolygonCollider2D>();*/
}
/*if(!isTouchingGround)
{
surfaceEffector2D.isSki = false;
}*/
NullReferenceException: Object reference not set to an instance of an object
SurfaceController.Surface () (at Assets/SurfaceController.cs:26)
SurfaceController.Update () (at Assets/SurfaceController.cs:21)
Im trying to fix this since 3 days but still couldnt. Please help.
If you should find yourself NOT doing one of the three steps, recognize that you are wasting time and the solution will get no closer until you do the steps above. That’s just how it is.
Referencing variables, fields, methods (anything non-static) in other script instances:
REMEMBER: it isn’t always the best idea for everything to access everything else all over the place. For instance, it is BAD for the player to reach into an enemy and reduce his health.
Instead there should be a function you call on the enemy to reduce his health. All the same rules apply for the above steps: the function must be public AND you need a reference to the class instance.
That way the enemy (and only the enemy) has code to reduce his health and simultaneously do anything else, such as kill him or make him reel from the impact, and all that code is centralized in one place.
Actually thank you for your answer im really happy to see someone trying to help me and i know this is the best way to help, this way i can fix my own problem and learn better and in the future i can handle better agaisnt these situations.
But even if i found the null one i still can’t fix this man
the variable in first script which name is isTouchingGround is seems null but i really cant find why is that?
It’s impossible for isTouchingGround to be null because it is a value type. Only reference types can be null. If this line of code is giving you an error:
Debug.Log(player.IsTouchingGround());
that means that player must be null. It’s important to know the difference between value and reference types/
I’m curious why you are trying to put the result of Physics2D.OverlapCircle into a boolean when none of the OverlapCircle results are boolean? The different versions of that method return either an int or a Collider2D, so perhaps that is why your boolean is null - because it can’t be allocated the type of the return from the OverlapCircle method?
You can set player in the inspector, but then after that you call GetComponent. It will replace player with whatever it finds. If the PlayerController is not attached to the same GameObject as SurfaceController then GetComponent will find nothing and it will set player to null. GetComponent can only find components attached to the GameObject that you call it from.