- Home /
Question by
Steviebops · Aug 14, 2013 at 06:25 PM ·
collisionprefabcomponentunexpected
Odd behaviour when instatiated
I'm using this script as part of a prefab, which I then instantiate in another script
using UnityEngine;
using System.Collections;
public class MyCollisionGenerator : MonoBehaviour
{
private GameObject headBone, weaponBone, bodBone;
private SphereCollider headSphere, weaponSphere, bodSphere;
GameObject[] Fighter;
public FighterScript char1Script, char2Script;
private bool failSafeVar = true;
void Start()
{
Fighter = GameObject.FindGameObjectsWithTag("Fighter");
foreach (GameObject bone in Fighter)
{
if (bone.name == "head")
{
headBone = bone;
headSphere = headBone.AddComponent<SphereCollider>();
headSphere.radius = .2f;
headSphere.isTrigger = true;
}
if (bone.name == "WeaponBone" || bone.name =="Weapon_R")
{
weaponBone = bone;
weaponSphere = weaponBone.AddComponent<SphereCollider>();
weaponSphere.radius = .5f;
weaponSphere.isTrigger = true;
}
if (bone.name == "spine1")
{
bodBone = bone;
bodSphere = bodBone.AddComponent<SphereCollider>();
bodSphere.radius = .3f;
bodSphere.isTrigger = true;
}
}
}
void Update()
{
if (weaponSphere.bounds.Intersects(bodSphere.bounds))
{
if (failSafeVar == true)
{
if (animation.IsPlaying("AnnaHammerJab") &&
animation["AnnaHammerJab"].time > .5f)
{
failSafeVar = false;
Debug.Log("-1 health");
}
}
}
else
{
failSafeVar = true;
}
}
}
If I pull the prefab character into the scene, the code works fine, I can even instantiate then player, and pull an opponent in.
When I instantiate the enemy, however, the code stops working, despite the enemy having all the correct spheres for collision.
Can anyone see why?
PS - I edited some old code out of this for clarity.
Comment
Answer by IgorAherne · Aug 14, 2013 at 06:49 PM
Not going to help you with player, but move the lines with GetComponent from Update to the end of Start to greatly improve productivity