Sorry i was in the wrong forums first .
i got some errors . I want to destroy my player , i use this
using UnityEngine;
using System.Collections;
public class PlayerController : MonoBehaviour {
public float speed;
private int count;
public GUIText countText;
public GUIText winText;
public int health = 100;
void start()
{
count =0;
SetCountText();
winText.text ="";
}
voidFixedUpdate()
{
float moveHorizontal =Input.GetAxis("Horizontal");
float moveVertical =Input.GetAxis("Vertical");
Vector3 movement =newVector3(moveHorizontal,0.0f,moveVertical);
rigidbody.AddForce(movement * speed *Time.deltaTime);
}
void OnTriggerEnter(Collider other) {
if(other.gameObject.tag =="PickUp")
{
other.gameObject.SetActive(false);
count = count +1;
SetCountText();
audio.Play();
}
}
void SetCountText() { countText.text="Count: " + count.ToString(); if (count >= 12) { winText.text = "YOU WIN!"; } }
voidOnCollisionEnter(Collision col){
if(col.transform.tag =="Enemy"){
Destroy(gameObject);
}
}
}
and enemy -
using UnityEngine;
using System.Collections;
public class EnemyAI: MonoBehaviour { public Transform target; public int moveSpeed; public int rotationSpeed;
privateTransform myTransform;
publicint damage =25;
publicGameObject otherGameObject;
privatePlayerController playerScript;
voidAwake(){
myTransform = transform;
}
// Use this for initialization
voidStart(){
GameObject go =GameObject.FindGameObjectWithTag("Player");
target = go.transform;
}
// Update is called once per frame
voidUpdate(){
Debug.DrawLine(target.position, myTransform.position,Color.cyan);
//look at target
//move towards target
Vector3 directionTowardsPlayer =(target.position - myTransform.position).normalized;
rigidbody.AddForce(directionTowardsPlayer * moveSpeed *Time.deltaTime);
}
}
i got this error - MissingReferenceException: The object of type ‘GameObject’ has been destroyed but you are still trying to access it. Your script should either check if it is null or you should not destroy the object. CameraController.LateUpdate () (at Assets/Scripts/CameraController.cs:17)
If i add this one in my CameraController.cs in LateUpdate() Function
if (gameObject != null)
{
// Do something
Destroy(gameObject);
}
then Console said “There are no audio listeners in the scene. Please ensure there is always one audio listener in the scene”
and my camera didn’t show anything
and if i add in enemy nothing happens it will continuously saying about my first error if the player destroy .
Please help me
You are destroying the game object that contains your camera. Thats why you are getting the audio listener error. Thats also why Unity cant render anything because there are no cameras in scene.
using UnityEngine;
using System.Collections;
public class CameraController : MonoBehaviour {
public GameObject player;
private Vector3 offset;
// Use this for initialization
void Start () {
offset = transform.position;
}
// Update is called once per frame
void LateUpdate () {
transform.position = player.transform.position + offset;
}
}
no but i i do something like that then they also show me that same two errors that i got first .
MissingReferenceException: The object of type ‘Transform’ has been destroyed but you are still trying to access it.
Your script should either check if it is null or you should not destroy the object.
UnityEngine.Transform.get_position () (at
MissingReferenceException: The object of type ‘GameObject’ has been destroyed but you are still trying to access it.
Your script should either check if it is null or you should not destroy the object.
CameraController.LateUpdate () (at Assets/Scripts/CameraController.cs:22)
using UnityEngine;
using System.Collections;
public class PlayerController : MonoBehaviour
{
public float speed;
private int count;
public GUIText countText;
public GUIText winText;
public int health = 100;
void start()
{
count = 0;
SetCountText();
winText.text = "";
}
void FixedUpdate ()
{
float moveHorizontal = Input.GetAxis("Horizontal");
float moveVertical = Input.GetAxis("Vertical");
Vector3 movement = new Vector3(moveHorizontal,0.0f,moveVertical);
rigidbody.AddForce (movement * speed * Time.deltaTime);
}
void OnTriggerEnter(Collider other)
{
if(other.gameObject.tag == "PickUp")
{
other.gameObject.SetActive(false);
count = count + 1;
SetCountText();
audio.Play();
}
}
void SetCountText()
{
countText.text="Count: " + count.ToString();
if (count >= 12)
{
winText.text = "YOU WIN!";
}
}
}
EnemyAI(This is my enemy controller script)
using UnityEngine;
using System.Collections;
public class EnemyAI: MonoBehaviour
{
public Transform target;
public int moveSpeed;
public int rotationSpeed;
public int health = 100;
private Transform myTransform;
public GameObject player;
private PlayerController playerScript;
void Awake(){
myTransform = transform;
}
// Use this for initialization
void Start () {
GameObject go = GameObject.FindGameObjectWithTag("Player");
target = go.transform;
}
// Update is called once per frame
void Update () {
if(player != null)
{
Debug.DrawLine(target.position, myTransform.position, Color.cyan);
//look at target
//move towards target
Vector3 directionTowardsPlayer = (target.position - myTransform.position).normalized;
rigidbody.AddForce (directionTowardsPlayer * moveSpeed * Time.deltaTime);
}
}
void OnCollisionStay(Collision col){
if(col.transform.tag == "Player"){
health-=2;
if(health<=0)
{
Destroy(col.gameObject);
}
}
}
}
Change you enemy AI start and update methods to this:
// Use this for initialization
void Start () {
player = GameObject.FindGameObjectWithTag("Player");
}
// Update is called once per frame
void Update () {
if(player != null)
{
Debug.DrawLine(player.transform.position, transform.position, Color.cyan);
//look at target
//move towards target
Vector3 directionTowardsPlayer = (player.transform.position - transform.position).normalized;
rigidbody.AddForce (directionTowardsPlayer * moveSpeed * Time.deltaTime);
}