Unity3d 4.6 javascript prefab with script
Hi I created a prefab named block1 and added a script on them named sheepController which does following:
var startingHealth:int = 2;
var currentHealth:int;
var isDead:boolean;
function Awake () {
currentHealth = startingHealth;
}
public function HitSheep (sheep:GameObject) {
Debug.Log("Hit Sheep startet");
Debug.Log("Current Health: " + currentHealth);
if (isDead)
{
Debug.Log("Sheep is dead");
return;
}
//currentHealth = currentHealth - 1;
currentHealth -= 1;
Debug.Log("Current Health: " + currentHealth);
if (currentHealth <= 0)
{
Debug.Log("Current Health <= 0");
Destroy(sheep);
Debug.Log("Destroy Sheep");
isDead = true;
}
}
After that I instantiate the prefab (block1) in another script:
function createBlocks()
{
for(var j:int = -5; j<6; j++)
{
Instantiate(block1, Vector3(j*21, 20, 0), Quaternion.identity);
}
}
On Collision I like to subtract health of block1 by 1 each hit until it reached <= 0 .Then the block should be destroyed.
function OnCollisionEnter(col:Collision)
{
if (col.gameObject.tag == "block1")
{
Debug.Log("Hit Block1");
audio.PlayOneShot(bricksound,0.2);
var scontroller:sheepController;
scontroller = GameObject.FindGameObjectWithTag("block1").GetComponent(sheepController);
if (col.gameObject != null)
{
scontroller.HitSheep(col.gameObject);
} else {
Debug.Log("col.gameObject is null!");
}
if (gameController.score >= 0)
{
gameController.score = gameController.score + 10;
}
}
}
The problem now the prefab count is not set for each prefab. It means if count is less then 0 on prefab hit it will be destroyed. I like to have each prefab has a start by 2 and subtracting by 1. Means two hits until destroy.
What I'm doing wrong? Thanks for help...
Answer by Bilelmnasser · Oct 09, 2015 at 02:16 PM
ths problem is here line 9 :
scontroller = GameObject.FindGameObjectWithTag("block1").GetComponent(sheepController);
EDIT : replace it with :
scontroller = collision.gameObject.GetComponent<sheepController>();
GameObject.FindGameObjectWithTag will search for first block in your scene got this tag and in that case you always call HitSheep of one Block if you have many block in your scene !!
Best to use the generic GetComponet over the one that accepts a string or type as a parameter. In the non-generic case it will return type component, this is not what is desired based off the strongly typed variable.
collision.gameObject.GetComponent(sheepController); // this will return type of Component
Should be:
collision.gameObject.GetComponent.<sheepController>(); // this will return type of sheepController
Just a suggestion
Thanks, work fine now with: col.gameObject.GetComponent(sheepController);
Your answer
Follow this Question
Related Questions
Create gameobject in other scene... 0 Answers
Im having an issue when Instantiate Prefab with specific rotation 2 Answers
Place prefab with mouse on to a grid based surface with a correct orientation. c# 0 Answers
Can't Instantiate an object as child of another 1 Answer
Cannot convert UnitEngine.Vector3 to UnityEngine.Transform when using .postition 1 Answer