Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by DkSker · Oct 27, 2019 at 06:45 AM · raycastnullreferenceexceptioninstantiate prefab

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

163 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges