Blood instantiating and detections...

Hello people, I’ve been working over a roach and I recently started coding it’s AI…
I’m in progress and i’ve got a few problems with some of the code…

First of all, thats the code (C#):

using UnityEngine;
using System.Collections;

public class AI_roach : MonoBehaviour 
{
  public float distenemy;
  public bool combat=false;
  public float movespeed =5;
  public float rotationSpeed= 5;
  public float cooldown=0;
  public float hittype=0;
  public bool spotted = false;
  public float health = 100;
	
  void Start()
	{
		GameObject bloodfab = GameObject.Find("effect_blood");
		Destroy(bloodfab,0);
	}
  void Awake()
	{
			animation["walk"].speed = 1;
			animation["attack2"].speed = 0.7f;
		    animation["attack1"].speed = 0.7f;
	}
	
  void Update()
	{
	    
		GameObject enemy = GameObject.Find("character_system");
		distenemy = Vector3.Distance(enemy.transform.position,transform.position);
		
		if(combat && character_stats.health>0)
		{
		animation.Stop("walk");
		Combat();
		}			
		
		if(distenemy>3 && spotted)
		{
			combat=false;
			 WalkToEnemy();
		}
		else if(distenemy<=3 && spotted)
		{
			animation.Stop("walk");
			combat=true;
		}
	    else
		Rest();
		
		
		if(distenemy<10)
		{
			transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(enemy.transform.position - transform.position), rotationSpeed *Time.deltaTime);

			GameObject ply = GameObject.Find("character_system");

			 if (!Physics.Linecast (transform.position, ply.transform.position)&& !spotted)
			 {
				 spotted=true;
			 }
			 
		}
	}
	
	void WalkToEnemy()
	{
		transform.position+=transform.forward * movespeed * Time.deltaTime;
		animation.Play("walk");
	}
	
	void Rest()
	{
		animation.Play("idle");
	}
	
	// _____________COMBAT_________________
	//_____________________________________
	//_____________________________________
	
	void Combat()
	{
		if(cooldown>0)
			cooldown-=1*Time.deltaTime;
		if(cooldown<=0)
		{
			hittype=Random.Range(0,4);
			if(hittype==3)
			ThrowHit();
			if(hittype<3)
				RegularHit();
		}
		if (!animation.isPlaying)
        animation.Play("idle");
		
		if(character_stats.health<=0)
		combat=false;
		
	}
	
	void RegularHit()
	{
		animation.Play("attack2");
		if(distenemy<3)
		{
			GameObject ply = GameObject.Find("character_system");
			GameObject bloodfab = GameObject.Find("effect_blood");
		    bloodfab = Instantiate(bloodfab, ply.transform.position , ply.transform.rotation) as GameObject;
			character_stats.health-=Random.Range(3,15);
		}
		cooldown=1.5f;
		
	} 
	
	void ThrowHit()
	{
		
		
		animation.Play("attack1");
		if(distenemy<3)
		{
		GameObject ply = GameObject.Find("character_system");
		GameObject bloodfab = GameObject.Find("effect_blood");
		bloodfab = Instantiate(bloodfab, ply.transform.position , ply.transform.rotation) as GameObject;
		character_stats.health-=Random.Range(12,31);
	    char_movesys.moveDir.y=15;
		}
		
		cooldown=3;
		
	}
}

It’s in progress… Now ive got two problems. The first is the blood instantiating… It types NullReferenceException every time the roach is about to instantiate the blood (The hittin’).
Ive been trying to call the “gameobject” at the beggining of the code but when the prefabs code destroys itself once the gameObject var “looses” the prefab…

The sec prob is that the roach cannot detect the char when it is sppoused to be but only in some different types of rotations and positions… Like something blocks the raycast…

Many thanks for those who can help me out (=

You’re doing it wrong; the bloodPrefab should be a public variable, which you would set in the Inspector - no Find or similar things. You should also have a variable to store the target, and set it in Start using Find - this function should never be used in Update or other frequently called routine, because it’s too slow.

The whole thing is something like this:

public GameObject bloodPrefab; // drag the blood prefab here
GameObject target; // fill this in Start

void Start(){
    // find the target only once at Start:
    target = GameObject.Find("character_system");
}

void Update(){
    // use target directly in your routines:
    distenemy = Vector3.Distance(target.transform.position,transform.position);
    ...
}

void RegularHit(){
    animation.Play("attack2");
    if(distenemy<3)
    {   // instantiate the blood this way:
        Instantiate(bloodPrefab, target.transform.position , target.transform.rotation);
        character_stats.health-=Random.Range(3,15);
    }
    cooldown=1.5f;
}
 ...

NOTE: You probably will get disappointed with the blood effect: you’re instantiating the blood at the target position, what will give weird results. You should find the hit point and normal to know the position and rotation where instantiate the blood. Take a look at this answer to know some details about this matter.

Hmm, it is still not working, after the prefab destroys itself it sets bloodfab as “Missing (GameObject)” and the roach starts vibrating it’s animation while re-reading the script over and over.

Please help me out, I’m pretty sure that this post is old and low but I need the answer and I dont wanna open another post ):