- Home /
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;
}
}
}
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. :)
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
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!
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;
}
}
}
Your answer
Follow this Question
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