- Home /
How would I attached a gameobject to a prefab that is spawned at a specific time?
I have the following script to make a ladybird (fly) attach a gameobject to follow its path. In the script, the gameobject is not attaching to the ladybird in the application during play, even though the gameobject is assigned and a debug logger states that the gameobject is active. Even though the gameobject is active, I don't see the object in the play view or in the editor view.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Berry.Utils;
[RequireComponent(typeof(Chip))]
public class Ladybird : IBomb, IAnimateChip, IChipLogic {
public static List<Slot> targetStack = new List<Slot>();
// public location for starting point
public Slot starting = new Slot();
// public objects
public GameObject FlyTorVid;
// public boolean for grid locations
public bool Top;
public bool TopRight;
public bool TopLeft;
public bool Bottom;
public bool BottomRight;
public bool BottomLeft;
public bool Left;
public bool Right;
public static bool ladybirdfly;
public bool flylock = false;
Chip _chip;
int birth; // Event count at the time of birth SessionAssistant.main.eventCount
int branchCount;
public Transform directionSprite;
Slot target;
public string seed = "";
public Chip chip {
get {
return _chip;
}
}
void OnDestroy() {
if (target != null && targetStack.Contains(target)) {
targetStack.Remove(target);
}
}
void Awake() {
_chip = GetComponent<Chip>();
birth = SessionAssistant.main.eventCount;
AudioAssistant.Shot("LadybirdCreate");
// Video GameObject FlyTorVid turned off
//FlyTorVid.SetActive (false);//false
}
float speed;
// Coroutine destruction / activation
public IEnumerator Destroying() {
if (birth == SessionAssistant.main.eventCount) {
chip.destroying = false;
yield break;
}
chip.busy = true;
chip.gravity = false;
chip.Play("Flying");
AudioAssistant.Shot("LadybirdCreateBee");
//stop the audio file when boolean event changes
foreach (SpriteRenderer sr in GetComponentsInChildren<SpriteRenderer>())
sr.sortingLayerName = "Foreground";
if (chip.slot) {
FieldAssistant.main.BlockCrush(chip.slot.coord, false);
FieldAssistant.main.JellyCrush(chip.slot.coord);
}
chip.ParentRemove();
Vector3 startPosition = chip.transform.position;
Vector3 lastPosition = transform.position;
Vector3 tagetPosition;
if (!target)
target = FindTarget();
speed = Random.Range(3f, 4f) / Vector3.Distance(startPosition, target.transform.position);
if (ladybirdfly) {
Debug.Log ("slow speed");
if (Vector3.Distance (target.transform.position, startPosition) <= 1.0f) {
speed = Random.Range (0.3f, 0.3f) / Vector3.Distance (startPosition, target.transform.position);
}else if (Vector3.Distance (target.transform.position, startPosition) <= 3.0f && Vector3.Distance (target.transform.position, startPosition) > 1.0f){
speed = Random.Range (1f, 1f) / Vector3.Distance (startPosition, target.transform.position);
}else if (Vector3.Distance (target.transform.position, startPosition) <= 5.0f && Vector3.Distance (target.transform.position, startPosition) > 3.0f) {
speed = Random.Range (2f, 2f) / Vector3.Distance (startPosition, target.transform.position);
} else {
speed = Random.Range (3f, 3f) / Vector3.Distance (startPosition, target.transform.position);
}
}
Vector3 normal = (target.transform.position - startPosition).normalized;
normal = new Vector3(-normal.y, normal.x, 0);
Debug.Log (Vector3.Distance(target.transform.position, startPosition));
normal *= Vector3.Distance(target.transform.position, startPosition) * Random.Range(0.2f, 0.2f); // 0.2f, 0.4f
if (Random.value > 0.5f)//0.5
normal *= -1; //-1
float time = 0f; // time = 0f;
float angle = 0; // time < 1
while (time < 1) {
time += Time.deltaTime * speed;
tagetPosition = target.transform.position + normal * Mathf.Sin(Mathf.PI * time);
transform.position = Vector3.Lerp(startPosition, tagetPosition, EasingFunctions.easeInOutQuad(time));
angle = Vector3.Angle(directionSprite.up, transform.position - lastPosition);
if (Vector3.Angle(-directionSprite.right, transform.position - lastPosition) > 90)
angle *= -1;
directionSprite.Rotate(0, 0, angle * Time.deltaTime * 15); // 15
lastPosition = transform.position;
yield return 0;
}
SessionAssistant.main.EventCounter();
Slot _target = target;
if (seed != "") {
Chip pu = FieldAssistant.main.AddPowerup(_target.coord, seed);
pu.busy = true;
_target.chip = pu;
yield return 0;
SessionAssistant.main.EventCounter();
}
Crush(_target.coord);
chip.busy = false;
chip.gravity = true;
AudioAssistant.Shot("LadybirdCrush");
SessionAssistant.main.EventCounter();
targetStack.Remove(target);
yield return new WaitForSeconds(0.1f);
chip.Play("Destroying");
AnimationAssistant.main.Explode(transform.position, 5, 7);
while (chip.IsPlaying("Destroying"))
yield return 0;
Destroy(gameObject);
}
Slot FindTarget() {
Slot result;
switch (LevelProfile.main.target) {
case FieldTarget.Block:
Block[] blocks =
Slot.all.Values.Where(x => x.block != null && x.block is Block && !targetStack.Contains(x))
.Select(x => x.block as Block).ToArray();
if (blocks.Length > 0) {
result = blocks[Random.Range(0, blocks.Length)].slot;
targetStack.Add(result);
return result;
}
break;
case FieldTarget.Color:
case FieldTarget.Jelly:
case FieldTarget.None:
case FieldTarget.Jam:
case FieldTarget.Duel:
case FieldTarget.SugarDrop: {
List<Chip> chips = new List<Chip>(FindObjectsOfType<Chip>());
chips = chips.FindAll(x => !x.busy).ToList();
int potential = -1;
int z = 0;
List<Chip> resultChip = new List<Chip>();
foreach (Chip c in chips) {
if (c.chipType == "Ladybird" || !c.destroyable)
continue;
if (c.destroying || !c.slot || targetStack.Contains(c.slot))
continue;
z = c.GetPotencial();
if (potential < z) {
resultChip.Clear();
potential = z;
}
if (potential == z)
resultChip.Add(c);
}
if (chip.jamType != "")
resultChip = resultChip.FindAll(x => Jam.GetType(x.slot) != chip.jamType).ToList();
if (resultChip.Count > 0) {
result = resultChip[Random.Range(0, resultChip.Count)].slot;
targetStack.Add(result);
return result;
}
break;
}
}
Slot[] targets = Slot.all.Values.Where(x => !targetStack.Contains(x) && x != chip.slot && x.chip != null).ToArray();
result = targets[Random.Range(0, targets.Length)];
targetStack.Add(result);
return result;
}
bool Crush(int2 coord) {
Slot s = Slot.GetSlot(coord);
FieldAssistant.main.BlockCrush(coord, false);
FieldAssistant.main.JellyCrush(coord);
if (s && s.chip) {
s.chip.SetScore(3f);
s.chip.jamType = chip.jamType;
s.chip.DestroyChip();
AnimationAssistant.main.Explode(s.transform.position, 3, 7);
}
return coord.IsItHit(0, 0, LevelProfile.main.width - 1, LevelProfile.main.height - 1);
}
public List<Chip> GetDangeredChips(List<Chip> stack) {
if (stack.Contains(chip))
return stack;
stack.Add(chip);
return stack;
}
#region Mixes
public void LadybirdsMix(Chip secondary) {
StartCoroutine(LadybirdsMixRoutine(secondary));
}
IEnumerator LadybirdsMixRoutine(Chip secondary) {
chip.busy = true;
chip.destroyable = false;
yield return 0;
List<Chip> ladies = new List<Chip>();
int count = 1;
if (secondary.chipType == "Ladybird")
count = 2;
if (chip.slot.jam)
chip.jamType = chip.slot.jam.type;
for (int i = 0; i <= count; i++) {
Chip l = ContentAssistant.main.GetItem<Chip>("Ladybird" + Chip.chipTypes[Random.value > 0.5f ? chip.id : secondary.id]);
l.destroyable = false;
l.transform.position = chip.slot.transform.position;
l.transform.localScale = Vector3.one;
l.transform.SetParent(Slot.folder);
l.transform.Find("LadybirdBody").rotation = Quaternion.Euler(0, 0, Random.Range(0f, 360f));
l.jamType = chip.jamType;
if (secondary.chipType != "Ladybird")
l.GetComponent<Ladybird>().seed = secondary.chipType;
ladies.Add(l);
}
chip.Minimize();
SessionAssistant.main.EventCounter();
foreach (Chip l in ladies) {
if (l == null) continue;
Animation a = l.GetComponent<Animation>();
while (a.isPlaying)
yield return 0;
l.destroyable = true;
l.jamType = chip.jamType;
l.DestroyChip();
}
chip.busy = false;
chip.HideChip(false);
}
// ******************* LadybirdsTor function for tornado
public void LadybirdsTor(Chip secondary) {
StartCoroutine(LadybirdsTorRoutine(secondary));
starting = chip.slot;
startingCoord (starting); Debug.Log (starting);
FlyTorVid.transform.position = starting.transform.position;
FlyTorVid.SetActive (true);
Debug.Log ("FlyTorVid is rendering on");
Debug.Log (FlyTorVid);
}
IEnumerator LadybirdsTorRoutine(Chip secondary) {
chip.busy = true;
chip.destroyable = false;
yield return 0;
List<Chip> ladies = new List<Chip>();
int count = 1;
if (secondary.chipType == "Ladybird")
count = 0;
ladybirdfly = true;
if (chip.slot.jam)
chip.jamType = chip.slot.jam.type;
for (int i = 0; i <= count; i++) {
Chip l = ContentAssistant.main.GetItem<Chip>("Ladybird" + Chip.chipTypes[Random.value > 0.5f ? chip.id : secondary.id]);
l.destroyable = false;
l.transform.position = chip.slot.transform.position;
l.transform.localScale = Vector3.one;
l.transform.SetParent(Slot.folder);
l.transform.Find("LadybirdBody").rotation = Quaternion.Euler(0, 0, Random.Range(0f, 360f)); // of 360f
l.jamType = chip.jamType;
ladies.Add(l);
}
chip.Minimize();
SessionAssistant.main.EventCounter();
foreach (Chip l in ladies) {
if (l == null) continue;
Animation a = l.GetComponent<Animation>();
while (a.isPlaying)
yield return 0;
l.destroyable = true;
l.jamType = chip.jamType;
l.DestroyChip();
}
chip.busy = false;
chip.HideChip(false);
}
public void startingCoord(Slot startpoint){
foreach (Side side in Utils.allSides){
Vector3 rotation = new Vector3();
switch (side) {
case Side.Bottom:
if (startpoint[side] != null){
Bottom = true;
}
break;
case Side.BottomRight:
if (startpoint[side] != null){
BottomRight = true;
}
break;
case Side.BottomLeft:
if (startpoint[side] != null){
BottomLeft = true;
}
break;
case Side.Left:
if (startpoint[side] != null){
Left = true;
}
break;
case Side.Top:
if (startpoint[side] != null){
Top = true;
}
break;
case Side.TopRight:
if (startpoint[side] != null){
TopRight = true;
}
break;
case Side.TopLeft:
if (startpoint[side] != null){
TopLeft = true;
}
break;
case Side.Right:
if (startpoint[side] != null){
Right = true;
}
break;
}
}
// vid location for Grid starting top left
if (Top == true && TopLeft == true && Left == true) {
TopLeft = true;
} else {
TopLeft = false;
}
if (Top == true && TopRight == true && Right == true) {
TopRight = true;
} else {
TopRight = false;
}
if (Right == true && BottomRight == true && Bottom == true) {
BottomRight = true;
} else {
BottomRight = false;
}
if (Left == true && BottomLeft == true && Bottom == true) {
BottomLeft = true;
} else {
BottomLeft = false;
}
// vid start location
if (TopLeft == true){
startpoint = startpoint[Side.TopLeft];
return;
}
if (TopRight == true){
startpoint = startpoint[Side.Top];
return;
}
if (BottomLeft == true){
startpoint = startpoint[Side.Left];
return;
}
if (BottomRight == true){
//startpoint = startpoint[side.BottomRight];
return;
}
}
// ******************* deploy ladybird ****************
public string[] GetClipNames() {
return new string[] { "Destroying", "Flying" };
}
public string GetChipType() {
return "Ladybird";
}
public bool IsMatchable() {
return true;
}
public int GetPotencial() {
return 15;// 15
}
#endregion
}
The line that says: FlyTorVid.SetActive (true); is the gameobject that I am setting active when the ladybird is moving as one gameobject after a combination of both ladybirds put together. The second count is "0", but I tried to alter it by changing it to "1" on the line: if (secondary.chipType == "Ladybird") count = 0; and still have no luck on rendering the gameobject.
Answer by tormentoarmagedoom · Aug 08, 2018 at 07:15 AM
Good day.
If you want us to help you, first you must help us...
You can not come and post 475 code lines and ask something about it..
If want a solution, reduce the problem to a simple question, abstarct all you can, find the exactly problem and try to make a question about that.
IF code says the object is active, it means is active. If you can not see it maybe is for render issues, its behind another object, is transparent... so many things!
Remake the post giving only the information we need, not 500 lines of code... And sometimes, an image (inspector and scene) is more value than lines and lines of code
Thanks! Bye!
This is the whole script. I can give small details, but then everyone is lost. All I am asking is, "is there a way to switch out prefabs at specific times", not a lecture on the process of posting. If you can help by looking at this script, then do so, if not, then please -- do not post anything.