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 DisneyFan05 · Jun 10, 2017 at 12:18 PM · animationscript.c #

2D UFO Game: How to Make Remaining Negative Pickups (Green) Disappear After All Positive Pickups (Yellow) Have Been Collected by the Player

alt text

Hey video game designers! I have another important question. I am working on an expansion of my 2D UFO Game for my Digital Arts class. This picture is a screenshot of my 2nd level I'm working on for this current exercise. Unfortunately, my teacher, who rarely verbally teaches us, has never taught us how to do code for gaming/players and just had us watch tutorials on unity3d.com to make the games for our past 3 assignments. However, there are no tutorials that I could find that mention how to make remaining objects that will sabotage the player in the game to disappear once the player has collected all of the positive pickups, because collecting all the positive pickups is the goal of each round. After all 12 yellow pickups are collected, I would like the red UFO in the picture and all the green pickups to disappear from the gameplay screen. It doesn't make sense for the player to collect all 12 yellow pickups, and then lose the level because he/she collided with the red UFO or because he/she collected 1+ green pickups. Please show me the accurate C Sharp code to write so all remaining objects that will affect the player negatively will disappear from the screen. Also, please leave any helpful comments or suggestions if you have any. Thank you and enjoy your spectacular summer!

screen-shot-2017-06-06-at-94819-pm.png (94.8 kB)
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 ShadyProductions · Jun 10, 2017 at 12:18 PM

When you generate these pickups you add them all to a list

Then everytime you touch one of the pickups it will find it in the list and you can then remove it from the list and set the object to inactive or possibly destroy it you can then do a quick LINQ statement over the list if there are still any gameobjects that have a tag 'positive' for example, if not (meaning u just removed the last one) then you can get all remaining values with tag 'negative' and remove these aswel (and put the gameobjects to inactive or destroy them).

Here is an example

 using System;
 using System.Collections.Generic;
 using System.Linq;
 using UnityEngine;
 
     //Put on empty gameobject with tag manager
     public class PickupHandler : MonoBehaviour
     {
         public List<GameObject> Pickups = new List<GameObject>();
         public GameObject NegativePickup;
         public GameObject PositivePickup;
 
         //generate pickups
         void Start()
         {
             for (int i = 0; i < 10; i++)
             {
                 var isEven = i % 2 == 0;
                 GameObject pickup;
 
                 if (isEven)
                 {
                     pickup = Instantiate(PositivePickup, new Vector3(i, i, 0), Quaternion.identity);
                     pickup.tag = "positive";
                 }
                 else
                 {
                     pickup = Instantiate(NegativePickup, new Vector3(i, i, 0), Quaternion.identity);
                     pickup.tag = "negative";
                 }
 
                 Pickups.Add(pickup);
             }
         }
     }

This will be another script called CollisionHandler (which you put on the ufo)

  using System;
  using System.Collections.Generic;
  using System.Linq;
  using UnityEngine;

 //put on the pickup gameobject
     public class CollisionHandler : MonoBehaviour
     {
         PickupHandler handler;
 
         void Start()
         {
             
             //Get the pickup handler script from the empty gameobject with tag 'manager'
             handler = GameObject.FindWithTag("manager").GetComponent<PickupHandler>();
         }
 
         //Some other class  that handles collision
         void OnTriggerEnter(Collider other)
         {
             if (other.gameObject.tag.Equals("positive", StringComparison.OrdinalIgnoreCase))
             {
                 other.gameObject.SetActive(false); // disable the object
 
                 if (handler.Pickups.Any(f => f.Equals(other.gameObject)))
                 {
                     //remove from the list
                     handler.Pickups.Remove(other.gameObject);
                 }
 
                 //do a quick check if we still have any 'positives left'
                 if (!handler.Pickups.Any(f => f.tag.Equals("positive", StringComparison.OrdinalIgnoreCase)))
                 {
                     //if we don't then remove all the negatives
                     foreach (var obj in handler.Pickups)
                     {
                         if (obj.tag.Equals("negative", StringComparison.OrdinalIgnoreCase))
                         {
                             handler.Pickups.Remove(obj); //remove it from the dictionary
                             obj.SetActive(false); //disable the gameobject
                         }
                     }
                 }
             }
         }
     }

Comment
Add comment · 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

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Game work in editor but not work on Android. 0 Answers

Can I make animations snap to a frame? 1 Answer

how to make an animation that can take value from script ? 0 Answers

Activate animation and Deactivate all unused animation 1 Answer

Animation of throw spear 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