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 /
  • Help Room /
avatar image
0
Question by vidit0210 · Jul 06, 2016 at 05:26 PM · c#

NullReferenceException: Object reference not set to an instance of an objectInt32)

My basic aim in this project when I hit on cube my console should say : Cube Hit

 using UnityEngine;
 using System.Collections;
 
 public class rotation : MonoBehaviour {
     public float speed =3f;
     public GameObject exPrefab;
     SpawnObjects so;
     // Use this for initialization
     void Start () {
         so = GetComponent<SpawnObjects> ();
     }
     
     // Update is called once per frame
     void Update () {
         transform.position -= new Vector3 (0.0f, speed, 0.0f) *Time.deltaTime;
         transform.Rotate (new Vector3 (15,30, 45) * Time.deltaTime);    
 
     }
 
 
     void OnMouseDown()
     {
         Destroy (this.gameObject);
         Instantiate (exPrefab, transform.position, Quaternion.identity);
         Check4Cube();
 
 
     }
 
     void Check4Cube()
     {
         foreach (GameObject go in so.prefabs) {
             if (go.tag == "Cube") {
                 Debug.Log ("Cube hit");
             } else {
                 Debug.Log ("NO");
             }
         }
     }
 
 
 
 }
 

Error : NullReferenceException: Object reference not set to an instance of an object rotation.Check4Cube () (at Assets/Assets/Scripts/rotation.cs:32) rotation.OnMouseDown () (at Assets/Assets/Scripts/rotation.cs:25) UnityEngine.SendMouseEvents:DoSendMouseEvents(Int32)

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 Jessespike · Jul 06, 2016 at 07:00 PM

As you're aware, Check4Cubes has a for-each loop iterating through prefabs in "so.prefabs".

So I have to ask, does the cube have a SpawnObjects component attached? And does the prefabs array contain objects? Because that seems to be what the error is referring to.

Although, it seems funny that each spawned object will contain another SpawnObjects script. Intended? Usually the way is to have one ObjectPoolManager or SpawnManager (SpawnObjects) and the objects are independent. Not each object containing another spawn manager. The simplest solution is to remove the Check4Cube function, you can assume the object being hit is the one calling OnMouseDown.

  void OnMouseDown()
  {
      Destroy (this.gameObject);
      Instantiate (exPrefab, transform.position, Quaternion.identity);

      //Check4Cube();
      Debug.Log("Cube hit"); 
  }
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 vidit0210 · Jul 07, 2016 at 05:24 PM 0
Share

yes prefabs array contain Object:

 using UnityEngine;
 using System.Collections;
 
 public class SpawnObjects : $$anonymous$$onoBehaviour {
 
     public GameObject[] prefabs;
     private float nextPrefabTime=0.0f;
     private float spawnRate=1.5f;
     public float timeLeft;
     int score=0;
     public Camera cam;
 
 
     // Use this for initialization
     void Start () {
         if (cam == null) {
             cam = Camera.    main;
         }
         
         Vector3 upperCorner = new Vector3 (Screen.width, Screen.height, 0.0f);
 
         Vector3 targetWidth = cam.ScreenToWorldPoint (upperCorner);
     }
 
 
 
 
     // Update is called once per frame
     void Update () {
         
     
             if (nextPrefabTime < Time.time) {
                 StartCoroutine (Spawn ());
                 nextPrefabTime = Time.time + spawnRate;
 
                 //speed up Spawn Rate
                 spawnRate *= 0.9f;
                 spawnRate = $$anonymous$$athf.Clamp (spawnRate, 0.3f, 99f);
             }
 
     }
 
     public IEnumerator Spawn()
     {
         yield return new WaitForSeconds (1.0f);
 
         GameObject pre = prefabs [Random.Range (0, prefabs.Length)];
 
         Vector3 spawnPosition = new Vector3 (transform.position.x + Random.Range (-11f, 11f), transform.position.y, transform.position.z);
 
         Instantiate (pre, spawnPosition, Quaternion.identity);
     }
 }
 

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

178 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

Related Questions

Move the character automaticly with the road 0 Answers

adjust image size with mouse 0 Answers

Setting Hierarchical slider parameters via script 0 Answers

WebCamTextures square 0 Answers

List does not contain a definition for Except error. 2 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