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 /
avatar image
0
Question by brandonhotdo · Jul 30, 2014 at 12:46 AM · photonnullreferenceexceptionnullnull reference exceptionalternative

NullReference

Here is my code

 using UnityEngine;
 using System.Collections;
 
 public class SolarSystemCreate : Photon.MonoBehaviour {
     public GameObject GasGiants;
     public GameObject Planets;
     float PlanetX;
     float PlanetY;
     float PlanetZ;
     float GasGiantX;
     float GasGiantY;
     float GasGiantZ;
     int PlanetCount;
     int GasGiantCount;
     int IsGasGiant;
     int IsPlanet;
     // Use this for initialization
     void Start () {
         IsGasGiant = Random.Range (1,30);
         IsPlanet = Random.Range (1,10);
         PlanetCount = Random.Range (1,20);
         GasGiantCount = Random.Range (1,3);
         int i = 0;
         int h = 0;
         // If IsPlanet is equal to 1 spawn planets
         if(IsPlanet == 1){
         while (i < PlanetCount) {
                         PlanetX = transform.position.x + Random.Range (-20384, 20384);
                         PlanetY = transform.position.y + Random.Range (-30, 30);
                         PlanetZ = transform.position.z + Random.Range (-20384, 20384);
                         GetComponent<PhotonView> ().RPC ("PlanetCreate",PhotonTargets.All);
                         i++;
                 }
         }
         //If IsGasGiant is equal to 1 spawn GasGiants
         if (IsGasGiant == 1) {
             while(h < GasGiantCount){
                 GasGiantX = transform.position.x + Random.Range (-20384, 20384);
                 GasGiantY = transform.position.y + Random.Range (-30, 30);
                 GasGiantZ = transform.position.z + Random.Range (-20384, 20384);
                 GetComponent<PhotonView> ().RPC ("GasGiantCreate",PhotonTargets.All);
                 h++;
             }
         }
         //fin
     }
     
     // Update is called once per frame
     void Update () {
     
     }
     [RPC]
     public void PlanetCreate () {
         Instantiate(Planets, new Vector3 (PlanetX, PlanetY, PlanetZ), Quaternion.identity);
     }
     [RPC]
     public void GasGiantCreate (){
         Instantiate (GasGiants, new Vector3 (GasGiantX, GasGiantY, GasGiantZ), Quaternion.identity);
     }
 }

What is suppose to happen is that it spawns in the planets and gas giants if the int GasgiantCount and PlanetCount is equal to one. But when it is equal to one it gives the error: NullReferenceException: Object reference not set to an instance of an object SolarSystemCreate.Start() (at Assets/Engine/SolarStstem/SolarSystemCreate.cs:31) I read that NullReferenceException is usually when something is getting a null (or nothing) and it needs a alternative code for when it does. But how can I get a alternative null code? By the way the errors are on line 31 and 41. Thanks for the help!

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by tyrike thompson · Jul 30, 2014 at 01:01 AM

You're getting a "NullReferenceException" on lines 41 and 31. Maybe it's because the component you are trying to receive (PhotonView) is not attached to the gameObject this script is attached to.

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
avatar image
0

Answer by Bunny83 · Jul 30, 2014 at 01:02 AM

Well, the error is pretty straight forward: You try to access the PhotonView component which should be attached to the same object as this script is attached to, but there isn't such a component! That's why you get a null-ref-error when you try to execute the "RPC" method of that component which doesn't exist.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Null Ref Exeption 1 Answer

NullReferenceException: Object reference not set to an instance of an object ProgressBar.Start () 2 Answers

NullReferenceException: Object reference not set to an instance of an object... 0 Answers

Best practice handling deleted object refences during SceneManager.LoadScene 0 Answers

NullReferenceException with UNET void 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