Question by
Numonic · Dec 22, 2017 at 05:53 PM ·
clonemissingreferenceexception
Missing Component in a Clone
How do I add a missing component to a Clone object in Unity? I'm currently using C# for the programming language.
MissingComponentException: There is no 'AudioSource' attached to the "FootSolder(Clone)" game object, but a script is trying to access it. You probably need to add a AudioSource to the game object "FootSolder(Clone)". Or your script needs to check if the component is attached before using it.`enter code here`
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[RequireComponent(typeof(AudioSource))]
public class FootDoDamage : MonoBehaviour
{
GameManager gameManager;
CharacterStats characterStats;
PlayerController playerController;
public Animator anim;
public GameObject explosionPrefab;
public AudioSource footExplosionSound;
public EnemySight enemySight;
public GameObject punchAttackObj;
public GameObject kickAttackObj;
public int footHealth = 100;
void Start()
{
if(explosionPrefab ==null)
{
explosionPrefab = GetComponent<GameObject>();
}
if(footExplosionSound == null)
{
footExplosionSound = GetComponent<AudioSource>();
}
footExplosionSound = GetComponent<AudioSource>();
if (anim == null)
{
anim = GetComponent<Animator>();
}
if (playerController == null)
{
playerController = GetComponent<PlayerController>();
}
if (enemySight == null)
{
enemySight = GetComponent<EnemySight>();
}
if (gameManager == null)
{
gameManager = GetComponent<GameManager>();
}
if (characterStats == null)
{
characterStats = GetComponent<CharacterStats>();
}
}
// Update is called once per frame
void Update()
{
}
public void HidePunchAttack()
{
enemySight.anim.SetBool("attackcombo", false);
punchAttackObj.SetActive(false);
}
public void ShowPunchAttack()
{
enemySight.anim.SetBool("attackcombo", true);
punchAttackObj.SetActive(true);
}
public void HideKickAttack()
{
enemySight.anim.SetBool("attackcombo", false);
kickAttackObj.SetActive(false);
}
public void ShowKickAttack()
{
enemySight.anim.SetBool("attackcombo", true);
kickAttackObj.SetActive(true);
}
public void FootTakeDamage()
{
if(this.gameObject != null)
{
// Taking away health based on sword attack
footHealth -= 50; // take away 50 from attack
anim.SetBool("hit", true);
anim.SetBool("attackcombo", false);
anim.SetBool("walk", false);
gameObject.SetActive(true);
if (footHealth <= 0)
{
anim.SetBool("hit", false);
anim.SetBool("defeated", true);
CancelInvoke("FootSolder");
StartCoroutine(HideFootSolder()); // Call the coroutine here and hide object
enemySight.navMeshAgent.updateRotation = false;
enemySight.navMeshAgent.angularSpeed = 0;
enemySight.navMeshAgent.isStopped = true;
//enemySight.navMeshAgent.enabled = false;
}
}
}
// Hide object in Coroutine instead of out right destroying it and causing errors.
IEnumerator HideFootSolder()
{
yield return new WaitForSeconds(2f);
footExplosionSound.Play();
Instantiate(explosionPrefab, transform.position, Quaternion.identity);
gameObject.SetActive(false);
footExplosionSound.Stop();
}
void OnTriggerEnter(Collider other)
{
if (other.tag == "lkatana" || other.tag == "rkatana" || other.tag == "leorfootattack")
{
FootTakeDamage();
// To Do Get the Blocked Colliders to turn off when players are defeated
print("Leo is hitting FootSolder.");
}
else
{
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityStandardAssets.Utility;
public class GameManager : MonoBehaviour {
public Transform []spawnPosition;
public GameObject footPrefab; //footPrefab2, //footPrefab3;
// private GameObject FootSolder(Clone);
public List<Transform> enemiesSpawned = new List<Transform>();
public FollowTarget followTarget;
public CameraFollow cameraFollow;
public GameObject blockColliders;
public bool wave1;
public bool wave2;
public bool wave3;
public int spawnRateMax = 6;
public int spawnRateMin = 1;
public float spawnTime = 3f;
public float countdown;
private float currentTime;
void Awake ()
{
currentTime = countdown;
// InvokeRepeating("Wave1Attack", spawnTime, spawnTime);
if(cameraFollow == null)
{
cameraFollow = GetComponent<CameraFollow>();
}
if(blockColliders ==null)
{
blockColliders = GetComponent<GameObject>();
}
if(followTarget == null)
{
followTarget = GetComponent<FollowTarget>();
}
}
// Update is called once per frame
void Update ()
{
/*
countdown -= Time.deltaTime;
if(countdown <= 0)
{
wave1 = true;
Wave1Attack();
// camera stops following player when a wave is on
cameraFollow.enabled = false;
wave1 = false;
//print("time is up.");
}
else
{
wave1 = false;
CancelInvoke("Wave1Attack");
cameraFollow.enabled = true;
}
*/
/*
if(wave1 )
{
wave1 = true;
Wave1Attack();
}
if(!wave1)
{
wave1 = false;
CancelInvoke("Wave1Attack");
}
*/
}
public void BlockCollidersOn()
{
blockColliders.SetActive(true);
followTarget.enabled = true;
}
public void BlockCollidersOff()
{
blockColliders.SetActive(false);
followTarget.enabled = false;
}
public void SpawnEnemies()
{
int ranValue = Random.Range(spawnRateMin, spawnRateMax +1);
for (int i = 0; i < ranValue; i++)
{
int ranSpawn = Random.Range(0, spawnPosition.Length);
GameObject Clone = Instantiate(footPrefab, spawnPosition[ranValue].position, Quaternion.identity) as GameObject;
//GameObject Clone
//enemiesSpawned.Add(Clone.transform);
enemiesSpawned.Add(Clone.transform);
}
}
/*
public void Wave1Attack()
{
int spawnPointIndex = Random.Range(0, spawnPosition.Length);
Instantiate (footPrefab, spawnPosition[spawnPointIndex].position, spawnPosition[spawnPointIndex].rotation);
}
*/
}
Comment
Your answer
Follow this Question
Related Questions
How can you load next the level after you destroy the last clone c# 1 Answer
Instantiate question 0 Answers
Fitting an instantiated prefab inside a cube? 0 Answers
Passing variables from one script to all clones 1 Answer
Unity_5.6.1f1 >> Unity_5.5.0f3 - Animation problems (missing reference)... 0 Answers