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 /
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
0
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
Add comment · 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

@koray

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
0

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
Add comment · 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 ;)

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

Instantiation happens three times, but called only once 1 Answer

Is it necessary to assign the result of Instantiate() to a variable and is there a need to always typecast? 1 Answer

instantiate object and let instantiated object know what it got instantiated from 2 Answers

IgnorePhysics on BoxCollider at GameObject instantiation 0 Answers

scripts gets disabled when the prefab is instantiated 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