i am trying to make door open when i shoot the target in 2D but as title says it doesnt work. In specialPipe prefab i got children that is pipe and it has animation to pull down that pipe that acts as a door. Only first object works but his clones immediately opens door(animation activates immediately instead when its trigered). sorry for bad english.
Target script that has animation:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TargetScript : MonoBehaviour
{
public Animator anim;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
anim = GameObject.FindGameObjectWithTag("BottomPipe").GetComponent<Animator>();
anim.enabled = false;
}
private void OnTriggerEnter2D(Collider2D collision)
{
anim.enabled = true;
Destroy(gameObject);
}
}
spawner script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PipeSpawnScript : MonoBehaviour
{
public GameObject pipe;
public GameObject specialPipe;
public float spawnRate = 2;
private float timer = 0;
public float heightOffset = 10;
// Start is called before the first frame update
void Start()
{
spawnPipe();
}
// Update is called once per frame
void Update()
{
if(timer < spawnRate)
{
timer += Time.deltaTime;
}else
{
spawnPipe();
timer = 0;
}
}
void spawnPipe()
{
float lowestPoint = transform.position.y - heightOffset;
float highestPoint = transform.position.y + heightOffset;
float randomNumber = Random.Range(0, 10);
if(randomNumber < 2)
Instantiate(pipe, new Vector3(transform.position.x, Random.Range(lowestPoint, highestPoint), 0), transform.rotation);
else
{
GameObject test = Instantiate(specialPipe, new Vector3(transform.position.x + 10, Random.Range(lowestPoint, highestPoint), 0), transform.rotation);
Animator anim = test.GetComponentInChildren<Animator>();
anim.Rebind();
}
}
}
I googled and found that Animator.rebind() should fix that but it doesnt work for me. Hope to find answer and ty in advance.
[FindGameObjectWithTag] returns the first GameObject it finds with the specified tag. [...] there is no guarantee this method will return a specific GameObject.
When you're script is running and searching for a "BottomPipe" tagged object Unity goes down the hierarchy tree checking every object for that tag. If finds the original object and disables the animation. The next instance of your "TargetScript" does the same thing. It always reaches the original object, says "mission accomplished" and peaces out, thus never deactivating your animation when it should.
In your case you should use GetComponentInChildren, since the pipe with the animator component is a child of the object that is running your "TargetScript".
If for some reason you have multiple children with animator components (and this at risk of the first problem occuring again), you can first pick out the child object that you want with Transform.GetChild and then get the component using the classic Component.GetComponent approach.