-1

UPDATED CODE

In my 3d Unity game I am trying to add in a canvas once all the collectibles are collected that says "Level completed... loading next level" and then loads the next level but once all the collectibles are collected, the game still plays. The script is successfully disabling the canvas but its not re-enabling it once the 5 collectibles are collected and the score reaches 5. Please help. Heres a screenshot of my script.

Collectible Script

Ruzihm
  • 19,749
  • 5
  • 36
  • 48
  • Code that is referenced in a question should be included as text. Images are not sufficient. – Ruzihm Dec 07 '21 at 22:51

1 Answers1

0

The problem is because you are destroying the object with the script before running the code that enables the canvas, because your script is attached to your collectable. Try disabling the mesh renderer and collider of your pickup, then destroying it after the code has run.

private MeshRenderer meshRenderer; // the mesh renderer attached to  the player
private Collider collider; // pickup's collider

private void Start ()
{
    meshRenderer = GetComponent<MeshRenderer>(); // get the mesh renderer component from the pickup (put this in the start function)
    collider = GetComponent<Collider>(); // get the collider attached to the pickup 
}

In your OnTriggerEnter function instead of using Destroy (gameObject), use meshRenderer.enabled = false and collideer.enabled = false. Then at the end of your OnTriggerEnter function, you can use Destroy (gameObject).

Also you don't need a separate canvas for every UI element. You can just have 1 canvas that contains all the different menus/elements and enable each of those.

Antoine
  • 141
  • 1
  • 12
  • I just added this to my code and changed it slightly and it still doesn't work. Now I can re collect the same object again and again cause they don't delete (I have 5 collectible objects). If you want to see new code I can add it for you. – Renee Johnston Jun 13 '20 at 05:42
  • Sorry I didn't consider that, you also need to disable the collider on the collectable. I'll update the answer now. – Antoine Jun 13 '20 at 05:46
  • Thank you. 2 more questions - where do I add the script? to my canvas and all of my collectibles? and also you want me to disable the collider on all of my collectibles? sorry im so bad at Unity and have been struggling with this all day. – Renee Johnston Jun 13 '20 at 05:52
  • Did it work? What is your script attached to now? I assumed that your script was attached to each of your collectables because it was called collectable script and it had Destroy (gameObject). If this script is on each collectable, it would disable the mesh renderer and collider of **only** the object that was collected, so it would look like the game object was destroyed. To directly answer your question add this script to all your collectables and not the canvas. – Antoine Jun 13 '20 at 05:59
  • It's still not working. I am not able to pick up the collectibles and the score is stuck on 0. Im not sure what else to do. I disabled my box colliders on all of my 5 collectibles and have my script on just my collectibles now. Is anything about that wrong? ill also add an updated screenshot of my code in my question so you can double check it. – Renee Johnston Jun 13 '20 at 06:01
  • Hi, where was your script originally before I answered your question? Have you looked at my updated answer? (I updated my original post with an improved answer" I didn't mean to start with the colliders disabled, but have the script disable the colliders. **Do not add this to your canvas.** – Antoine Jun 13 '20 at 06:05
  • Ok I saw your new code and I know what you've done wrong. Basically on line 17 add `collider.enabled = false`. In the inspector have your collider enabled by default. Remove the script from your canvas and make sure it's on all of your pickups. – Antoine Jun 13 '20 at 06:10
  • Hey. Still not working. The collectibles still don't add to score or delete. Also on line 10 there's a green line that says "CollectVirus.collider" hides inherited member "Component.collider". No clue what that means but it might change something?? Do you want me to show you anything else? – Renee Johnston Jun 13 '20 at 06:17
  • Hmm. Can you update your script (in text not image), your hierarchy, and the inspectors of your pickups and canvas. – Antoine Jun 13 '20 at 06:20
  • Im not sure what that means... how do I do that? sorry. – Renee Johnston Jun 13 '20 at 06:24
  • I meant update the question you asked by posting your full script in text, as well as your ScoringSystem script. Also attach a picture of your hierarchy, and pictures of the inspector of your pickups and canvas :) – Antoine Jun 13 '20 at 06:26