- Home /
Unique prefabs?
I have a script of formations that my enemy ships go to but they go to a prefab called "4 Slot Formation" I want to have like 10 of these on the map and my enemies go to the formations that are empty till all are full of their slots, I've got the logic on how they go to 1 of them, but I don't really know how to make them go to other ones because they are not unique, and or I don't know how to script them to all work together. Here is my script of how the fighters go to the "4 Slot Formation"
using UnityEngine;
using System.Collections;
public class Fighter_AI : MonoBehaviour {
// Access Formation AI
public GameObject script; // Access for getcomponent<Formation_AI>
// Smooth Move to formation
public float smoothTranslateSpeed = 0.3F; // Speed for going from spawn to formation with lerp
public Transform playerTarget; //Target for the enemies to target the player and lookat
public Transform playerDist; // Going to use this for mutants they will freely come to the player but always stop at a certain distance, maybe it can be a random range too so it looks interesting
public Transform fightFormTransform; // Think this is for the formation transforms
// Setup for the sockets
public Transform FormTarg1; // Formation socket1
public Transform FormTarg2; // Formation socket2
public Transform FormTarg3; // Formation socket3
// Variables
public bool emtyS1;
public bool goToSlot01 = false; // For the logic
public bool goToSlot02 = false; // For the logic
public bool goToSlot03 = false; // For the logic
// Use this for initialization
void Start () {
playerTarget = Player_Controller.playerTransform; // The player target declaration
playerDist = Player_Controller.playerTransform; // Player Distance declaration
fightFormTransform = Formation_AI.FormTarget; // formation target declaration
// Targets for the Sockets
FormTarg1 = Formation_AI.Form_Slot1;
FormTarg2 = Formation_AI.Form_Slot2;
FormTarg3 = Formation_AI.Form_Slot3;
}
void Awake()
{
script = GameObject.Find("4 Slot Formation");
//Formation_AI script = GameObject.Find("4 Slot Formation").GetComponent<Formation_AI>();
if (script.GetComponent<Formation_AI>().isEmptySlot1)
{
script.GetComponent<Formation_AI>().isEmptySlot1 = false;
goToSlot01 = true;
goToSlot02 = false;
goToSlot03 = false;
}
else if (script.GetComponent<Formation_AI>().isEmptySlot2)
{
script.GetComponent<Formation_AI>().isEmptySlot2 = false;
goToSlot01 = false;
goToSlot02 = true;
goToSlot03 = false;
}
else if (script.GetComponent<Formation_AI>().isEmptySlot3)
{
script.GetComponent<Formation_AI>().isEmptySlot3 = false;
goToSlot01 = false;
goToSlot02 = false;
goToSlot03 = true;
}
}
// Update is called once per frame
void Update () {
transform.LookAt(playerTarget);
if (playerDist)
{
float dist = Vector3.Distance(playerDist.position, transform.position);
//Debug.Log("Distance to other: " + dist);
}
if (goToSlot01)
{
GoToSlot01();
}
else if (goToSlot02)
{
GoToSlot02();
}
else if (goToSlot03)
{
GoToSlot03();
}
}
// Moves the ships to the right spots. May need to replace the target with a variable that gets adjusted by how many formations are added
void GoToSlot01()
{
transform.position = Vector3.Lerp(transform.position, FormTarg1.position, Time.deltaTime * smoothTranslateSpeed);
}
void GoToSlot02()
{
transform.position = Vector3.Lerp(transform.position, FormTarg2.position, Time.deltaTime * smoothTranslateSpeed);
}
void GoToSlot03()
{
transform.position = Vector3.Lerp(transform.position, FormTarg3.position, Time.deltaTime * smoothTranslateSpeed);
}
}
Answer by Michael Covert · Oct 11, 2012 at 12:27 AM
I'd use GameObject.FindGameObjectsWithTag() to find all of your formations (of course, you'd have to tag your formations). This returns an array of all your formation variables; you can then proceed to cycle through your array until you find an empty spot and then assign your fighter to that slot.
Something like:
GameObject[] allSpots = GameObject.FindGameObjectsWithTag("Formations");
for(int i = 0; i < allSpots.Length; ++i)
{
if( /* your logic to determine if a spot is free */ )
{
//assign that spot
break;
}
}
Does that make sense?
Also, I'd avoid calling GameObject.FindGameObjectsWithTag() more often than you need to; maybe store this in a variable that only gets updated when new formations are created - but that's something you can worry about more once you have your base functionality working.
Yeah that somewhat makes sense, but How do i access duplicate variables from duplicate prefabs that way? script.GetComponent().isEmptySlot1 seems to be all the formations ins$$anonymous$$d of specific ones.
you'd do something like allSpots[i].GetComponent().isEmptySlot1
Hmm thanks, that almost worked, now I just need to figure out changing to the other transform targets of the next formation, I would guess the same thing except with transforms of the spawn sockets I want?
Answer by clakes707 · Oct 10, 2012 at 10:58 PM
I think what you would want to do is set up a system of checks with variables. When a ship is added to the first prefab, set it counter variable to + 1. When it reaches its max, make the ships go to the next prefab, and so on.
I don't even know how to find the next prefab as unique though? Because they have the same exact name. It needs to be dynamic too because I'm going to randomly spawn these formations on pre placed sockets all around the map at certain times.
Your answer
![](https://koobas.hobune.stream/wayback/20220613082152im_/https://answers.unity.com/themes/thub/images/avi.jpg)