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 GeekyHuff · Oct 21, 2014 at 06:08 PM · c#playermouserandom

GameObject clones won't add a point.

Alright, i have a simple 2d game, where you click a button it should add a point. So i have a random spawner at the top, which is just ping poning back and fourth dropping an object. I just want to click the gameobject and add a point. Now i can do that once, then after i click the object once i get this error.

MissingReferenceException: The object of type 'GameObject' has been destroyed but you are still trying to access it. Your script should either check if it is null or you should not destroy the object. UnityEngine.Object.Instantiate (UnityEngine.Object original) (at C:/BuildAgent/work/d63dfc6385190b60/artifacts/EditorGenerated/UnityEngineObject.cs:91) SpawnMultipleObjects.SpawnRandomObject () (at Assets/Scripts/SpawnMultipleObjects.cs:32) SpawnMultipleObjects.Update () (at Assets/Scripts/SpawnMultipleObjects.cs:52)

Okay now here is my 2 scripts i have, the one that spawns the objects. Then the other one that Destroys the object and adds a point. Like i said, all i want is to know what i need to do, to make it not say this error and when i click on the gameobject clones it adds a point.

  1. using UnityEngine; using System.Collections;

      public class SpawnMultipleObjects :
         MonoBehaviour {
         
             private Vector3 startPosition;
         
             private float newXPos = 0f;
         
             public float moveSpeed = 1f;
         
             public float moveDistance = 4f;
         
             public GameObject[] gameObjectSet;
         
             public float timeLeftUntilSpawn =
         0f;     public float startTime = 0f;
         
             public float secondsBetweenSpawn =
         3f;
         
             // Use this for initialization
             void Start () {         startPosition =
         transform.position;
         
             }
         
             void SpawnRandomObject() {
                         int whichItem = Random.Range (0, 2);
                 
                         Debug.Log ("Our random number is " + whichItem); 
                 
                         GameObject myObj = Instantiate (gameObjectSet [whichItem]) as
         GameObject;
                 
                         myObj.transform.position = transform.position;         }
         
             // Update is called once per frame
             void Update () {          // newXPos++;
         
                 newXPos = Mathf.PingPong(Time.time
         * moveSpeed, moveDistance) - (moveDistance / 2f) ;
         
                 transform.position = new Vector3
         (newXPos, startPosition.y,
         startPosition.z);
         
                 timeLeftUntilSpawn = Time.time -
         startTime;
         
                 if (timeLeftUntilSpawn >=
         secondsBetweenSpawn) {             startTime
         = Time.time - Random.Range (0.1f, 0.5f);             timeLeftUntilSpawn = 0;             // Debug.Log ("Spawn one here"):
         
                     SpawnRandomObject();         }
         
             } }
    
    
    
    

=================================================================================================

 using UnityEngine;
 using System.Collections;
 
 public class DestroyOnTouch : MonoBehaviour {
     
     int myScore = 0;
     
     public GUIText scoreText;
     
     
     public void OnMouseDown () {//Work with touch
         
         Destroy(gameObject);
         
         myScore = myScore + 1;
         
     }
     
     public void OnDestroy () {
         
         scoreText.text = "Score: " + myScore;
         
     }
 }


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

0 Replies

· Add your reply
  • Sort: 

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

2 People are following this question.

avatar image avatar image

Related Questions

Trying to make a Point system that references a Prefab. 1 Answer

Multiple Cars not working 1 Answer

How can I make the script to play with the mouse? 0 Answers

Distribute terrain in zones 3 Answers

Random Player Respawn Points 3 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