- Home /
Instantiate, null reference and Raycasting problem. 2D
So, i have a 2D zombie character that checks his facing direction using raycast and detecting using 4 different colliders around the raycast's starting point. The problem is when I instantiate the prefab, it gave me a null reference to the collider detection method which I created. The script is:
void checkDirection(Vector2 direction, Transform transform)
{
RaycastHit2D directionHit = Physics2D.Raycast(transform.position, direction * 10);
if (directionHit.collider.name == "left_Colliders")
{
attackDirection = 3;
faceDirection = 3;
}
if (directionHit.collider.name == "right_Collider")
{
attackDirection = 4;
faceDirection = 4;
}
if (directionHit.collider.name == "up_Collider")
{
attackDirection = 1;
faceDirection = 1;
}
if (directionHit.collider.name == "down_Collider")
{
attackDirection = 2;
faceDirection = 2;
}
}
private void Update()
{
checkDirection(direction, this.transform);
}
which was called in the update function.
The code above was attached to my zombie main game object. There is one child collider containing 4 other child colliders to detect the 4 sides named left_colliders, right_colliders... so on.
The directions work fine. I have an empty game object with spawning enemy script attached to it:
public GameObject enemyPrefab;
public float full_range = 50f;
private Transform player;
private float playerMinX;
private float playerMaxX;
private float playerMinY;
private float playerMaxY;
private Vector3 playerPosition;
public float randomX;
public float randomY;
void Start()
{
player = GameObject.Find("Player").transform;
}
void Update()
{
playerMinX = player.position.x - full_range / 2;
playerMaxX = player.position.x + full_range / 2;
playerMinY = player.position.y - full_range / 2;
playerMaxY = player.position.y + full_range / 2;
randomX = Random.Range(playerMinX, playerMaxX);
randomY = Random.Range(playerMinY, playerMaxY);
playerPosition = new Vector3(randomX, randomY, 0);
Vector3 viewPosition = Camera.main.WorldToViewportPoint(this.gameObject.transform.position);
if (randomX > viewPosition.x + 1 && randomY > viewPosition.y + 1)
{
Instantiate(enemyPrefab, playerPosition, Quaternion.identity);
}
}
}
(ignore the fact that it is spawning every frame per second, I will fix that later this is just a test) the script above is the script spawning my enemies around my character.
Everytime I start the game, it spawns the enemies correctly and the facing directions work just fine, however it only gives me 13-21 null reference error after those numbers, it stopped giving me the errors despite the game still spawning more in.
Answer by DkSker · Oct 27, 2019 at 08:02 AM
Nvm, all i had to do was call the method in the fixedupdate instead of update.
Your answer
Follow this Question
Related Questions
(2D) Raycast check - null reference even when (RayHit != null) 2 Answers
Why am I getting NullReferenceException: Object Reference... 1 Answer
Need some help with JS raycasting hitting a certain tagged object 1 Answer
NullReferenceException when an object is in the way that is not the player 1 Answer
My Raycast on awake fires and will not change Help! 0 Answers