Hello,
I added some capsules to my game and destroy them on trigger just to make my game little bit more interesting. However i get this error when i play the game. I did not add rigidbody to capsules because i dont want physic rules to be applied to them. But somehow my script triyng to reach theirs rigidbody. How can i fix this bug ?
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class Rocket : MonoBehaviour
{
public AudioClip SoundToPlay;
[SerializeField] float thrustPower = 100f;
[SerializeField]float rcsThrust = 100f;
AudioSource audioSource;
Rigidbody rigidBody;
enum State { Alive, Dying, Transcending}
State state = State.Alive;
void Start()
{
rigidBody = GetComponent<Rigidbody>();
audioSource = GetComponent<AudioSource>();
}
// Update is called once per frame
void Update()
{
if (state == State.Alive)
{
Rotate();
Thrust();
}
else
{
audioSource.Stop();
//rigidBody.isKinematic = true;
//rigidBody.Sleep();
}
}
void OnCollisionEnter(Collision collision)
{
if(state != State.Alive) { return; }
switch (collision.gameObject.tag)
{
case "Friendly":
print("OK");
break;
case "Finish":
state = State.Transcending;
Invoke("LoadNextScene", 1f);
break;
default:
state = State.Dying;
print("hit something deadly");
Invoke("LoadDeathScene", 1f);
break;
}
}
private void LoadDeathScene()
{
SceneManager.LoadScene(0);
}
private void LoadNextScene()
{
SceneManager.LoadScene(1);
}
private void OnTriggerEnter(Collider other)
{
if(other.gameObject.tag == "Pill")
{
//audioSource.PlayOneShot(SoundToPlay, 1f);
Destroy(other.gameObject);
}
}
private void Rotate()
{
rigidBody.freezeRotation = true;
float rotateThisFrame = rcsThrust * Time.deltaTime;
if (Input.GetKey(KeyCode.A))
{
transform.Rotate(Vector3.forward * rotateThisFrame);
}
else if (Input.GetKey(KeyCode.D))
{
transform.Rotate(Vector3.back*rotateThisFrame);
}
rigidBody.freezeRotation = false;
}
private void Thrust()
{
float thrustPowerChange = thrustPower * Time.deltaTime;
if (Input.GetKey(KeyCode.Space))
{
rigidBody.AddRelativeForce(Vector3.up * thrustPowerChange);
if (!audioSource.isPlaying)
{
audioSource.Play();
}
}
else
{
audioSource.Stop();
}
}
}