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 dragonrun1 · Apr 16, 2015 at 01:24 AM · c#beginner

destroy one clone of an object versus all(C#)

I'm new to programming and decided to try and make a game, not for release but more for my personal learning. At the moment I am trying to spawn workers if they reach a certain criteria of food and living space and if it falls under destroy them. the problem im having is figuring out how to destroy one clone and not all of them, is this possible or am i going about this wrong. bellow is my code for the span if needed.

 using UnityEngine;
 using System.Collections;
 
 public class Spawn : MonoBehaviour {
     // a being how many spaces of living there are and b being how much food is available
     public int a = 0;
     public int b = 0;
     //home and food representing what is required of each to spawn a new person
     public int home = 3;
     public int food = 2;
 
     public Rigidbody Villager;
     public Transform spawn;
 
     // Use this for initialization
     void Start()
     {
         //timer to increment a and b evry 30 sec
     }
 
     // Update is called once per frame
     void Update()
     {
         if (home == a && food == b)
         {
             //spawn character prefab at empty objects location
             Instantiate(Villager, spawn.position, spawn.rotation);
 
             //increasesing what is require to spawn the next person
             home += 3;
             food += 2;
         }
 
     }
 }

Comment
Add comment · Show 2
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 ashleyjlive · Apr 16, 2015 at 02:04 AM 0
Share

So your end goal is this.

I have four variables. Two being the requirements. The other two being the current amount.

You want to know if both current amounts (food & living space) are greater than or equal to the requirements.

If they are, create a villager/worker, but in the process the villagers take up food and living space, so you must deduct the amount they use from the current amounts.

However, if you find yourself in negative amounts of food and living space you need to remove some villagers to get it in the positive.

Additionally, every 30 seconds we add some food and living space.

Please respond and tell me this is the idea you're going for so I can create a script. :)

avatar image dragonrun1 · Apr 16, 2015 at 02:13 AM 0
Share

You hit the head on the nail. Though now I've just decide to only make it to if food goes negative to remove one villager every X amount of time until food is positive again

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by ndrummer31 · Apr 16, 2015 at 05:21 AM

Hi dragonrun1! If you didn't care about which villager you're killing off, you could do:

 // Assuming the villagers have the tag "Villager"...
 Destroy ( GameObject.FindObjectWithTag( "Villager" ).gameObject );

If you're more particular, I would create a list of spawned villagers and you'd be able to pinpoint specific spawned villagers.

 using System.Collections.Generic;

 public class Spawn : MonoBehavior
 {
     public List<Rigidbody> spawnedVillagers;

     private void Start ()
     {
         spawnedVillagers = new List<Rigidbody>();
     }
     private void Update()
     {
         if (home == a && food == b)
         {
             spawnedVillagers.Add ( Instantiate (Villager, spawn.position, spawn.rotation ) as Rigidbody );

             home += 3;
             food += 2;
         }
     }
     public void DestroyVillager(int index)
     {
         if (spawnedVillagers[index] != null)
         {
             Destroy(spawnedVillagers[index].gameObject);
         }
     }
 }

I hope this helps even a little bit :)

Welcome to the coding community!

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
avatar image
0

Answer by ashleyjlive · Apr 16, 2015 at 02:18 AM

Something like this?

 using UnityEngine;
 using System.Linq;
 using System.Collections;
 using System.Collections.Generic;
 public class VillagerSpawn : MonoBehaviour
 {
     public int CurrentLivingSpaces = 0; //Current Space
     public int CurrentFood = 0; //Current Food
     [System.Serializable]
     public class Requirements
     {
         public int LivingSpaces = 3;
         public int Food = 2;
     }
     public Requirements requirements = new Requirements(); //Stores the requirements for a villager
     public GameObject Villager; //Villager prefab
     public Transform Spawn; //Spawn location
     public List<GameObject> VillagerList = new List<GameObject>(); //List of all the villagers
     void Start()
     {
         StartCoroutine(Timer()); //Starts the loop
     }
     IEnumerator Timer()
     {
         //An infinite loop coroutine that every 30 seconds adds 1 to each variable
         while (this.enabled)
         {
             yield return new WaitForSeconds(30f);
             CurrentLivingSpaces++;
             CurrentFood++;
         }
     }
     void Update()
     {
         if (CurrentLivingSpaces >= requirements.LivingSpaces && CurrentFood >= requirements.Food)
         {
             GameObject NewVillager = Instantiate(Villager, Spawn.position, Spawn.rotation) as GameObject;
             CurrentLivingSpaces -= requirements.LivingSpaces;
             CurrentFood -= requirements.Food;
             VillagerList.Add(NewVillager);
         }
         else if (CurrentLivingSpaces < 0 || CurrentFood < 0)
         {
             Destroy(VillagerList.Last());
             VillagerList.Remove(VillagerList.Last());
             CurrentLivingSpaces += requirements.LivingSpaces;
             CurrentFood += requirements.Food;
         }
     }
 }

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Play Animation several times 0 Answers

Get Rigidbody2D component in script (c#) 1 Answer

Problems with raycast 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