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 /
avatar image
0
Question by MakerDavid · May 13, 2016 at 10:48 AM · classduplicate

How to make duplicate unique?

So I have created a fully working quiz script attached to a small rotating sphere. When I collide with the sphere I get a quiz popping up on my screen and I have to answer the correct question.

Here is a gif of how the script works right now.

https://gyazo.com/8d0df7329ca0afe7ab5f12ac3dc559da

However, when I create a duplicate of this sphere. I am thrown a null pointer exception, and it doesn't work.

https://gyazo.com/4199d436f12190a5a03062f70492c274

Under you will see 2 of the 3 scripts I am using. I think the last one is irrelevant. It is just to make a timer work when Time.Scale = 0. Any help is very appreciated!

 using UnityEngine;
 using System.Collections;
 using UnityEngine.UI; 
 
 public class QuizManager : MonoBehaviour {
     public Question[] questions;
     [SerializeField]
     private GameObject Quiz;
     private Question currentQuestion;
     [SerializeField]
     private GameObject displayText;
     private bool clickTrue;
     private bool clickFalse;
     [SerializeField]
     private GameObject displayTimer;
     [SerializeField]
     private GameObject trueButton;
     [SerializeField]
     private GameObject falseButton; 
     private string correct = "Correct";
     private string fals = "Wrong"; 
     private static float delay = 2.0f;
     private bool Answer;
     [SerializeField]
     private GameObject knight; 
 
     void OnTriggerEnter(Collider col) {
         if (col.gameObject.name == "Knight") {
 
             knight.GetComponent<ClickMove>().enabled = false; 
             Quiz.SetActive(true);
             Time.timeScale = 0.0f; 
             getRandomQuestion();
             displayText.GetComponent<Text>().text = currentQuestion.fact;
 
             StartCoroutine("Counter", 1); 
 
         }
 
     }
 
     void getRandomQuestion()
     {
         int RandomIndex = Random.Range(0, questions.Length);
         currentQuestion = questions[RandomIndex]; 
     }
 
     public void TrueButton() {
         Answer = true; 
         clickTrue = true;
         trueButton.GetComponent<Button>().enabled = false;
         falseButton.GetComponent<Button>().enabled = false;
 
         if (currentQuestion.isTrue && clickTrue) //Nullpointer exception! Object reference not set to an instance of the object. 
         {
             StartCoroutine(WaitCorrect(delay));
 
         }
         else
         {
             StartCoroutine(WaitFalse(delay));
         }
 
     }
 
     public void FalseButton() {
         Answer = true; 
         clickFalse = true;
         trueButton.GetComponent<Button>().enabled = false;
         falseButton.GetComponent<Button>().enabled = false;
 
         if (!currentQuestion.isTrue && clickFalse)
         {
 
             StartCoroutine(WaitCorrect(delay));
 
         }
         else
         {
             StartCoroutine(WaitFalse(delay));
         }
 
     }
 
     
     private IEnumerator WaitCorrect(float delay)
     {
         
         yield return StartCoroutine(CoroutineUtilities.WaitForRealTime(delay-1));
         displayText.GetComponent<Text>().text = correct;
         yield return StartCoroutine(CoroutineUtilities.WaitForRealTime(delay));
         Quiz.SetActive(false);
         Time.timeScale = 1.0f;
         knight.GetComponent<ClickMove>().enabled = true;
         knight.GetComponent<Playerstat>().addHealthEnergy();
         DestroyObject(gameObject); 
     }
 
     private IEnumerator WaitFalse(float delay)
     {
 
         yield return StartCoroutine(CoroutineUtilities.WaitForRealTime(delay-1));
         displayText.GetComponent<Text>().text = fals;
         yield return StartCoroutine(CoroutineUtilities.WaitForRealTime(delay));
         Quiz.SetActive(false);
         Time.timeScale = 1.0f;
         knight.GetComponent<ClickMove>().enabled = true;
         DestroyObject(gameObject);
     }
 
     private IEnumerator Counter(float delay)
     {
         for (int Count = 8; Count >= 0; Count--)
 
         {
             if (Answer)
 
             {
                 StopCoroutine("Counter"); 
             }
 
             displayTimer.GetComponent<Text>().text = Count.ToString(); 
             yield return StartCoroutine(CoroutineUtilities.WaitForRealTime(delay));
         }
 
         Quiz.SetActive(false);
         Time.timeScale = 1.0f;
         knight.GetComponent<ClickMove>().enabled = true;
         DestroyObject(gameObject);
 
     }
 
 }


The quiz script.

 using UnityEngine;
 using System.Collections;
 
 [System.Serializable]
 public class Question {
 
     public string fact;
     public bool isTrue; 
     
 }
 




Comment
Add comment · Show 6
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 meat5000 ♦ · May 13, 2016 at 12:26 PM 0
Share

Please detail of the error itself. It gave you information about it which you seem to be withholding from us ;)

avatar image MakerDavid meat5000 ♦ · May 13, 2016 at 12:31 PM 0
Share

Hi, meat thanks for your replay. Object reference not set to an instance of an object at line 54.

avatar image meat5000 ♦ · May 13, 2016 at 12:48 PM 0
Share

Firstly, you cant really duplicate anything that has declared static fields or members. Statics are meant to be Unique and singular...and Global.

Secondly, what you experiencing is a loss of Reference.

Object A looks at Object B as it knows where to look. It has a reference, like an address.

Object B gets copied.

Object A still looks to Object B and knows nothing about Object B-clone as it has been provided with no new reference, no address. This is not automatic.

"Im going to send them a letter."

"Oh, you know theyve got a new building thats identical to the last one"

"So Ill send it there"

"You cant"

"Why not?"

"We dont know the address"

"Well then we'll have to LOO$$anonymous$$ IT UP"

https://www.google.co.uk/search?q=unity+finding+references&ie=utf-8&oe=utf-8&client=ubuntu&channel=fs&gfe_rd=cr&ei=0cw1V4bOJ5$$anonymous$$CaLbCmpAN&gws_rd=ssl

https://www.google.co.uk/search?q=unity+finding+references&ie=utf-8&oe=utf-8&client=ubuntu&channel=fs&gfe_rd=cr&ei=0cw1V4bOJ5$$anonymous$$CaLbCmpAN&gws_rd=ssl#channel=fs&q=unity+find

https://www.google.co.uk/search?q=unity+getcomponent&ie=utf-8&oe=utf-8&client=ubuntu&channel=fs&gfe_rd=cr&ei=As01V-CRAp$$anonymous$$CaLbCmpAN&gws_rd=ssl

https://www.google.co.uk/search?q=unity+getcomponent&ie=utf-8&oe=utf-8&client=ubuntu&channel=fs&gfe_rd=cr&ei=As01V-CRAp$$anonymous$$CaLbCmpAN&gws_rd=ssl#channel=fs&q=unity+passing+references

Hope this makes sense. I've given you the Generic answer. It should be enough to help you fix your code. Do come back if you dont get it. ($$anonymous$$aybe then I'll try the code).

avatar image MakerDavid meat5000 ♦ · May 13, 2016 at 12:57 PM 0
Share

Wait, if I didn't use drag and drop this would have fixed itself? If I used Gameobject.find, or findwithtag this wouldn't have been a problem? I don't 100% understand the explanation but I am taking a look at all the links you provided at this moment in time. All the https://gyazo.com/0fac099249fe2fe701e7c3127a95963e objects are linked to the copy as well so I don't quite understand :P I am sorry :(

avatar image meat5000 ♦ MakerDavid · May 13, 2016 at 01:06 PM 0
Share

You mean drag and drop in the editor, for variables? Yeah that is #1 reason why people experience NullRefs. Coding 'Find' or alternative methods of grabbing references to your scripts is the way to go for Reusable and versatile code. I must admit, I stopped dropping variables in Editor a very long time ago and never missed it one bit.

I dont think I've had a NullRef since.

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by meat5000 · May 13, 2016 at 04:34 PM

OK I've butchered your scripts for you.

The Gist of this is : If you feel like you should call something "Manager" there should probably be only 1 of them.

I separated out parts between two scripts.

QuizManager, which does what it did before (but modified) and QuizPrompt which is the new script to go on the Orb. The Orb pokes the QuizManager to do its job...and thats that. Note that the QuizPrompt sends a reference to the calling Orb so that the QuizManager can destroy it once its finished its process.

QuizManager script now goes on to an Empty GameObject called QuizManager (surprise surprise).

This approach means that you can now HardCode/ Drag And Drop your buttons etc in the Editor and have no fear of losing references.

Dont let the line

 Quiz = GameObject.Find("CanvasGroup").transform.Find("Quiz").gameObject;

baffle you. GO.Find doesnt find inactive objects but T.Find does so i found the parent with GO.Find and used T.Find to find the inactive Quiz child.

 using UnityEngine;
 using System.Collections;
 using UnityEngine.UI; 
 
 public class QuizManager : MonoBehaviour {
     public Question[] questions;
     [SerializeField]
     public GameObject Quiz;
     public Question currentQuestion;
     [SerializeField]
     private GameObject displayText;
     private bool clickTrue;
     private bool clickFalse;
     [SerializeField]
     private GameObject displayTimer;
     [SerializeField]
     private GameObject trueButton;
     [SerializeField]
     private GameObject falseButton; 
     private string correct = "Correct";
     private string fals = "Wrong"; 
     private static float delay = 2.0f;
     private bool Answer;
     [SerializeField]
     private GameObject knight;
     public GameObject currentOrbReference;
 
     void Start()
     {
         knight = GameObject.Find("Knight");
         Quiz = GameObject.Find("CanvasGroup").transform.Find("Quiz").gameObject;
     }
     
     public void FireMe() {
             Answer = false;
             knight.GetComponent<ClickMove>().enabled = false; 
             Quiz.SetActive(true);
             Time.timeScale = 0.001f; 
             getRandomQuestion();
             displayText.GetComponent<Text>().text = currentQuestion.fact;
             trueButton.GetComponent<Button>().enabled = true;
             falseButton.GetComponent<Button>().enabled = true;
             StartCoroutine("Counter", 1); 
         }
 
     public void getRandomQuestion()
     {
         int RandomIndex = Random.Range(0, questions.Length);
         currentQuestion = questions[RandomIndex]; 
     }
 
     public void TrueButton() {
         Answer = true; 
         clickTrue = true;
         trueButton.GetComponent<Button>().enabled = false;
         falseButton.GetComponent<Button>().enabled = false;
 
         if (currentQuestion.isTrue && clickTrue)
         {
             StartCoroutine(WaitCorrect(delay));
         }
         else
         {
             StartCoroutine(WaitFalse(delay));
         }
     }
 
     public void FalseButton() {
         Answer = true; 
         clickFalse = true;
         trueButton.GetComponent<Button>().enabled = false;
         falseButton.GetComponent<Button>().enabled = false;
 
         if (!currentQuestion.isTrue && clickFalse)
         {
             StartCoroutine(WaitCorrect(delay));
         }
         else
         {
             StartCoroutine(WaitFalse(delay));
         }
     }
 
     public  IEnumerator WaitCorrect(float delay)
     {
         yield return StartCoroutine(CoroutineUtilities.WaitForRealTime(delay-1));
         displayText.GetComponent<Text>().text = correct;
         yield return StartCoroutine(CoroutineUtilities.WaitForRealTime(delay));
         Quiz.SetActive(false);
         Time.timeScale = 1.0f;
         knight.GetComponent<ClickMove>().enabled = true;
         knight.GetComponent<Playerstat>().addHealthEnergy();
         Destroy(currentOrbReference); 
     }
 
     public  IEnumerator WaitFalse(float delay)
     {
         yield return StartCoroutine(CoroutineUtilities.WaitForRealTime(delay-1));
         displayText.GetComponent<Text>().text = fals;
         yield return StartCoroutine(CoroutineUtilities.WaitForRealTime(delay));
         Quiz.SetActive(false);
         Time.timeScale = 1.0f;
         knight.GetComponent<ClickMove>().enabled = true;
         Destroy(currentOrbReference); 
     }
 
     public  IEnumerator Counter(float delay)
     {
         for (int Count = 8; Count >= 0; Count--)
         {
             if (Answer)
 
             {
                 StopCoroutine("Counter"); 
             }
 
             displayTimer.GetComponent<Text>().text = Count.ToString(); 
             yield return StartCoroutine(CoroutineUtilities.WaitForRealTime(delay));
         }
         Quiz.SetActive(false);
         Time.timeScale = 1.0f;
         knight.GetComponent<ClickMove>().enabled = true;
         Destroy(currentOrbReference); 
     }
 }

 using UnityEngine;
 using System.Collections;
 using UnityEngine.UI; 

 public class QuizPrompt : MonoBehaviour {
 [SerializeField]
 private QuizManager quizManager;

 void Start()
 {
     quizManager = GameObject.Find("QuizManager").GetComponent<QuizManager>();
 }
 void OnTriggerEnter(Collider col)
 {
     if (col.gameObject.name == "Knight")
     {
         quizManager.currentOrbReference = this.gameObject;
         quizManager.FireMe();
     }
 }

}

Comment
Add comment · Show 5 · 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 meat5000 ♦ · May 13, 2016 at 04:39 PM 0
Share

By the way, when you export a Unity Package it tend to forget references. If you ZIP the entire Project folder, everything is retained.

If you cant get it to work, Ill send the project back to you, or I can screenshot the setup.

Also, use Destroy as DestroyObject is deprecated apparently.

avatar image MakerDavid meat5000 ♦ · May 13, 2016 at 04:56 PM 0
Share

Okey, thanks. While I don't 100% understand why I have to do this, I can see that now the Quizmanager "can" differentiate between the objects. I will definitely do something similar next time, I make something like this.

I see that you put Time.Scale = 0.001? and not 0, why? I think it worked good when it was 0? But if there is a reason that 0.001 is better I am ready to learn :)

avatar image meat5000 ♦ MakerDavid · May 13, 2016 at 04:59 PM 0
Share

It was just to make sure I could unpause it

https://www.google.co.uk/search?q=unity+timescale+0+problem&ie=utf-8&oe=utf-8&client=ubuntu&channel=fs&gfe_rd=cr&ei=kAc2V_kb14FotoCxgAU&gws_rd=ssl

All 'like' things should be kept together. It makes no sense to have many objects all handling the Quiz. The single Quiz$$anonymous$$anager should handle the Quiz, manage all the relevant objects (text etc) and only be subject to prompts from the activating object. It doesnt make sense for these other objects to be responsible for the workings of the Quiz in any other way than saying "Go".

Did you get it all working with the new scripts? If it works, click Accept ;)

Show more comments

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

43 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

Related Questions

Problem building for Android "Duplicate class.." 0 Answers

Parameter Name Need "Some Generator Here" is a duplicate 2 Answers

Class definition not recognized after load level 0 Answers

Instantiating gameobjects in an array / class problem | NullReferenceException: 0 Answers

Best way to get variable from another script 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