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 MajorSmurf · Aug 11, 2018 at 11:42 PM · error messageuser interfaceprefab-instance

Can someone explain this error?

Ok so i asked a question yesterday regarding why my final score wasn't displaying correctly but well I've now got a weird error to do with it. basically I'm using a c# level editor to create my levels and the endpoint when spawned as a prefab just doesn't like the code I've used. My code works fine when I have the object in the scene hierarchy but this is the current error I'm now getting and I would love some help. thank you very much for any help. all code and picture of error below:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 
 public class EndBlock : MonoBehaviour
 {
     private string endblock = "player";
 
     private GameObject fScreen;
 
     public bool gameEnd = false;
 
     void Start()
     {
         fScreen = GameObject.Find ("EndCardScreen") as GameObject;
         fScreen.gameObject.SetActive (false);
         Debug.Log("Found endcardscreen");
     }
 
     private void OnCollisionEnter(Collision col)
     {
         if (col.gameObject.tag == endblock) 
         {
             fScreen.gameObject.SetActive (true);
             gameEnd = true;
             Debug.Log("Displaying endcardscreen");
             Debug.Log ("You Win");
         }
     }
 }

alt text

unityerror.png (29.4 kB)
Comment
Add comment · Show 8
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 Pulkit30 · Aug 12, 2018 at 07:42 AM 0
Share

I think u have not given the reference to the game object . This kind of error usually occurs when u have initialized a variable ,used it but didn't give reference to the variable. Double click on the error it will show u the variable whose reference is missing.

avatar image Hellium · Aug 12, 2018 at 08:42 AM 0
Share

Can you copy-paste the full error message?

I don't believe the problem come from the given code.

Do you instantiate something somewhere? The error indicates a problem when calling InstantiateSingleWithParent. Are you sure you give a non-null`parent` when calling Instantiate?

avatar image JVLVince · Aug 12, 2018 at 10:41 AM 0
Share

Your question is too lack of information, please provide us more info so that we can help you easier

avatar image DanielleSisserman · Aug 12, 2018 at 11:02 AM 0
Share

I'm not understanding what you're asking...is something wrong with your level code? with spawning prefabs? with the score? what object in the scene hierarchy? you also said something about spawning but there's non of that in the code you posted...could you please try to rephrase your question and attach all relevant code.

avatar image ShroomWasTaken · Aug 12, 2018 at 11:49 AM 0
Share

Your question needs more information.

A guess would be that line 16:

 fScreen = GameObject.Find ("EndCardScreen") as GameObject;



fails because it can't find a GameObject called "EndCardScreen". $$anonymous$$ake sure there actually is a GameObject with that name in the scene. Also, why are you casting it to a GameObject? GameObject.Find already returns a GameObject type, you don't need to cast it.

avatar image MajorSmurf ShroomWasTaken · Aug 12, 2018 at 04:24 PM 0
Share

i know i don't need to cast it as a gameobject but its been giving me a lot of problems so i was trying things to verify for a fact that it was beco$$anonymous$$g a gameobject.

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by MajorSmurf · Aug 12, 2018 at 02:33 PM

Ok ill explain in more detail sorry for any confusion. This whole code is designed that when the player hits the gameobject called endblock it sends a bool value to display the endcardscreen. Now from I've noticed it finds the endcardscreen fine and I've debugged it and it displays the panel called endcardscreen fine however it either appears blank or just doesn't display sometimes. I'm missing something really basic and its annoying. I either get a null reference exception or the code works fine but just displays an empty screen like shown below. The worst part was that it was working and so heres all the code to do with this error. If you need more screenshots like me know cause limited to 2 screenshots per post

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 
 public class ScoreUI : MonoBehaviour 
 {
     [SerializeField]
     public static float score;
 
     [SerializeField]
     public static float timer;
 
     [SerializeField]
     public static float numberOfCollisions;
 
     public float FinalScore;
     public float targetTime;
 
     public Text scoreText;
     public Text timerText;
     public Text FScoreText;
     public Text FTimerText;
     public Text collisionNumberText;
     public Text FinalScoreText;
     public Text TargetTimeText;
 
     private EndBlock endblock;
 
     public GameObject GameEndingBlock;
 
     private bool visitedFScreen;
     void Start ()
     {
         scoreText.GetComponent<Text> ();
         timerText.GetComponent<Text> ();
         FScoreText.GetComponent<Text> ();
         FTimerText.GetComponent<Text> ();
         collisionNumberText.GetComponent<Text> ();
         TargetTimeText.GetComponent<Text> ();
         endblock = GameEndingBlock.GetComponent<EndBlock> ();
         visitedFScreen = false;
     }
     
     // Update is called once per frame
     void Update () 
     {
         timer += Time.deltaTime;
         timer = Mathf.Round (timer * 100f) / 100f;
         scoreText.text = score.ToString ();
         timerText.text = timer.ToString ();
 
         if (visitedFScreen == false) 
         {
             if (endblock.gameEnd == true) 
             {
                 DisplayFinalScore ();
             }
         }
     }
         
     void DisplayFinalScore()
     {
         FinalScore = (score + (targetTime - timer)) - numberOfCollisions;
         FScoreText.text = score.ToString ();
         FTimerText.text = timer.ToString ();
         TargetTimeText.text = targetTime.ToString ();
         collisionNumberText.text = numberOfCollisions.ToString ();
         FinalScoreText.text = FinalScore.ToString ();
         visitedFScreen = true;
         Debug.Log ("Target Time: " + targetTime);
         Debug.Log ("FinalScore: " + FinalScore);
         Debug.Log ("score: " + score);
         Debug.Log ("Number Of Collision: " + numberOfCollisions);
         Debug.Log ("Time Finished: " + timer);
     }
 }

alt text

and legit i just reopened to grab the code and screenshots and the error is gone but it appears and disappears but all my debugging shows that a) it finds the endcardscreen b) displays with zero info or c) i get an error like shown above. @nobx101 @DanielleSisserman @JVLVince @Hellium @Pulkit30

alt text


image1.png (255.5 kB)
image2.png (216.1 kB)
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 MajorSmurf · Aug 12, 2018 at 04:25 PM 0
Share
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class LevelGenerator : $$anonymous$$onoBehaviour {
 
     public Texture2D map;
     public ColorToPrefab[] colour$$anonymous$$appings;
     public Transform mapHolder;
     public GameObject Back;
 
     // Use this for initialization
     void Start () 
     {
         Generate$$anonymous$$ap ();
     }
 
     void Generate$$anonymous$$ap()
     {
         for (int x = 0; x < map.width; x++) 
         {
             for (int y = 0; y < map.height; y++) 
             {
                 GenerateTile (x, y);
             }
         }
     }
 
     void GenerateTile(int x, int y)
     {
         Color pixelColour = map.GetPixel (x, y);
         if (pixelColour.a == 0) 
         {
             return;
         }
 
         foreach (ColorToPrefab colour$$anonymous$$apping in colour$$anonymous$$appings) 
         {
             Vector3 backPosition = new Vector3 (x, y, 1.1f);
             Instantiate (Back, backPosition, Quaternion.identity, mapHolder);
 
             if (colour$$anonymous$$apping.colour.Equals (pixelColour)) 
             {
                 Vector3 position = new Vector3 (x, y, 0);
                 Instantiate (colour$$anonymous$$apping.prefab, position, Quaternion.identity, mapHolder);
             }
         }
         Debug.Log (pixelColour);
     }
 }

This is the code used to generate the level.

avatar image Spacejet13 · Aug 12, 2018 at 04:32 PM 0
Share

Don't use GetComponent<Text>();,as there are multiple text objects you are getting in this way. Assign them in the inspector, as how will unity know which Text object you are referring to when you are setting so many variables using GetComponent<Text>();!!

avatar image MajorSmurf · Aug 12, 2018 at 11:25 PM 0
Share

Ok I'm going to be as clear as possible.... this code 100% works as long as the game object endblock isn't a prefab which is a problem as i want the level to be generated from a png image to make levels easy and quick to design. Don't believe me look at this works 100% which is why I'm confused and need some guidance.

alt text

avatar image Spacejet13 MajorSmurf · Aug 15, 2018 at 03:25 PM 0
Share

Oh.. I See

avatar image MajorSmurf Spacejet13 · Aug 15, 2018 at 06:45 PM 0
Share

any solution? i was thinking maybe i need to put the scripts in a certain execute order? so the block prefab 100% spawns before the ui script

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

98 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

Related Questions

Prefab Canvas's buttons arn't working in different scene 4 Answers

Transform child out of bounds 0 Answers

Is it possible to use Windows Form Application to develope user interface in Unity? 1 Answer

PointerEventData pointerID inconsistency 1 Answer

Game shifts to lower right corner post build 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