Question by
Shekha · Sep 15, 2016 at 01:09 PM ·
collisioninstantiategamefirst person
After Instantiating my gameobject twice one of the 2 script doesn't work
Here are the two scripts for my bird in my bird shooter game i made as a beginner practice on my own.
The problem is after instantiating twice the AI file doesn't work only it changes position on collision as the col file compiles. Yes i have examined the inspector in attaching the files Yes i have tried recreating the prefeb /////
The Bird Collision file, i thought separating it might solve the problem and i'm still asking help.
using UnityEngine;
using System.Collections;
public class BirdCol : MonoBehaviour
{
public BirdAI xX; //accessing the variable
public GameObject Bird;
void OnCollisionEnter(Collision col)
{
if (col.gameObject.tag == "Bullet")
{
Destroy(gameObject);
xX.x = Random.Range(0, 3);
Instantiate(Bird, xX.Points[xX.x], Quaternion.identity);
print(xX.x);
}
}
}
//This is the basic AI
using System.Collections;
public class BirdAI : MonoBehaviour
{
public Vector3[] Points;
private int CurrPoint;
private int nextPoint;
public float speed;
public int x;
// Use this for initialization
void Start()
{
x = Random.Range(0, 3);
transform.position = Points[x];
}
// Update is called once per frame
void Update()
{
if (transform.position == Points[x])
{
x = Random.Range(0, 3);
}
if (transform.position != Points[x])
{
gameObject.transform.position = Vector3.MoveTowards(transform.position, Points[x], speed * Time.deltaTime);
// print (x);
}
}
}
Comment