• 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 /
This question was closed Apr 18, 2014 at 05:57 PM by lancer for the following reason:

I changed the group form 2 to 0, and new it works. Guess I don't fully understand the group ids.

Thanks for your time guys :)

avatar image
Question by lancer · Apr 18, 2014 at 05:22 PM · instantiationnullreferenceexeption

NullReferenceExeption

Hey guys,

So I'm writing a scrip for a game I'm working on, and I keep getting a NullReferenceExeption. I have been using unity for almost a year, but I still can't figure it out.

It's on line 24, the line is "BulletObject.rigidbody.velocity = transform.forward * BulletSpeed;"

 using UnityEngine;
 using System.Collections;
 
 public class FiringScript : Photon.MonoBehaviour {
 
     public KeyCode FiringKey = KeyCode.Space;
 
     public GameObject bullet;
 
     public GameObject[] FiringPoints;
 
     public int BulletSpeed = 10;
     // Use this for initialization
     void Start () {
     
     }
     
     // Update is called once per frame
     void Update () {
         if(photonView.isMine){
             if (Input.GetKeyDown (KeyCode.Space)) {
                 foreach (GameObject point in FiringPoints){
                     GameObject BulletObject = PhotonNetwork.Instantiate(bullet.name, point.transform.position, point.transform.rotation, 2);
                     BulletObject.rigidbody.velocity = transform.forward * BulletSpeed;
                 }
             }
         
         }
         
     }
 }
Comment

People who like this

0 Show 3
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
avatar image koray1396 · Apr 18, 2014 at 05:39 PM 0
Share

not sure but try;

 GameObject BulletObject = PhotonNetwork.Instantiate(bullet.name, point.transform.position, point.transform.rotation, 2) as GameObject;
avatar image lancer · Apr 18, 2014 at 05:52 PM 0
Share

@hiray

Tried it, didn't seem to change anything, it appears the object isn't being instantiated

avatar image lancer · Apr 18, 2014 at 05:57 PM 0
Share

I changed the group form 2 to 0, and new it works. Guess I don't fully understand the group ids.

Thanks for your time guys :)

1 Reply

  • Sort: 
avatar image

Answer by Kumo-Kairo · Apr 18, 2014 at 05:43 PM

Obviously if your PhotonNetwork.Instantiate(...) function returns null, there will be no object to work with and therefore you can't access any of it's properties like .rigidbody and so on.

Just for the sake of good programming style if you encounter any situation in which your object somehow could be null you should check it before accessing any of it's properties

 GameObject BulletObject = PhotonNetwork.Instantiate(bullet.name, point.transform.position, point.transform.rotation, 2);
 if(BulletObject != null)
     BulletObject.rigidbody.velocity = transform.forward * BulletSpeed;

Of course there might be a problem with your PhotonNetwork.Instantiate(...) function, you may need to check if it's called under right circumstances, but you should check if object is null anyway

Comment

People who like this

0 Show 1 · 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
avatar image lancer · Apr 18, 2014 at 05:49 PM 0
Share

Added that, and now I don't get the error. It appears the object isn't being instantiated, going to look into it.

Just trying koray1396's comment ;)

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

21 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

Related Questions

Using Transform on instantiated objects 1 Answer

Simple bullet instantiation problem 1 Answer

Awake() not called on script referenced directly from Project window 1 Answer

Help with Prefab script running on wrong instance... 0 Answers

The AI-Players do not see new install (Instantiate) object (the ball) after they set the first goal in gates and old object (the ball) was destroyed. 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