Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
  • Help Room /
avatar image
0
Question by cazoulable · Dec 23, 2020 at 04:52 PM · instantiateprefabreference

Call function for instantated prefab. "Object reference not set"

Hello !

First post on the forum, I just just started to play with Unity (I love it so far), but I'm a little bit lost sometimes.

My current problem is the following : I have a Player and who can shoot Bullet. For that I have created a prefab for each object, and associated a script for each prefab (`Player.cs` and Bullet.cs, respectivitely). I was doing it alright at first, but only in one direction (to the right). Now, I would like to shoot bullet in the direction based on the player position, so I had to refactor my code a little.

Ideally, I would like to call a Shoot function from the Player script, which will Instantiate a bulletPrefab, and call a Fly function from the Bullet script, which will take the player's position to decide the direction.

Here is Player.cs:

 public class PlayerController : MonoBehaviour
 {
     public bool isMovingForward = true;
     public GameObject bulletPrefab;

     private void Shoot()
     {
         GameObject bullet = Instantiate(bulletPrefab, ..., ...);
         Bullet bulletController = bullet.GetComponent<Bullet>();
         bulletController.Fly(isMovingForward);
     }
 }

And Bullet.cs

 public class Bullet : MonoBehaviour
 {
     private Rigidbody bulletRb;
 
     void Start() {bulletRb = GetComponent<Rigidbody>();}
  
     void Fly(bool isForward) {bulletRb.AddForce(getForceVector(), ForceMode.Impulse);}
 
     Vector3 getForceVector() {...}
 }
 

That ... doesn't work. I get NullReferenceException: Object reference not set to an instance of an object Bullet.Fly (System.Boolean isForward) (at Assets/Scripts/Bullet.cs:31)

Would you be able to help me understand what's going on ? Also, if you think I'm not doing it the right way, I'd love to here some suggestion, I'm very new to all of this.

Thanks !

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

Answer by cazoulable · Dec 23, 2020 at 05:18 PM


I solved my own problem, by getting the player direction in the bullet controller. I don't know why I wanted to make it "so" complicated...

 public class PlayerController : MonoBehaviour
 {
     public bool isMovingForward = true;
     public GameObject bulletPrefab;

      // Shoot
     private void Shoot()
     {
         Vector3 dropletSpawnPos = transform.position;
         GameObject bullet = Instantiate(bulletPrefab, dropletSpawnPos, bulletPrefab.transform.rotation);
     }
 }

and

 public class Bullet : MonoBehaviour
 {
     public bool isGoingFoward = true;
     private Rigidbody bulletRb;
     private PlayerController playerController;
 
     // Start is called before the first frame update
     void Start()
     {
         bulletRb = GetComponent<Rigidbody>();
         playerController = GameObject.FindGameObjectWithTag("Player").GetComponent<PlayerController>();
         isGoingFoward = playerController.isMovingForward;
     }
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

205 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 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

Instantiated Object Has Wrong Rotation 0 Answers

is the Character Controller a Collider? 2 Answers

Instantiating an object with an Emissive material 0 Answers

How to make 2d shooting with instantiated bullets 0 Answers

instantiated prefab doesn't follow spawn rotation 1 Answer


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