So basically, I am trying to make a tower defense style game with little or no help, kinda trying to use what I have learned. I find my self stuck, so I try to do some homework and ask questions, instead of just taking a tutortial. I feel I learn more this way …
But…
Here’s the problem. I am trying to make this turret script where I have two functions:
1 - To instantiate an object when the tower detects a collision with a gameobject that is tagged “enemy”.
2 - Function needs to return the transform of the enemy gameobject, to know where to Instantiate.
This is as far as I have made it, can anyone give some feedback or why what I have done so far is right, or wrong.
#pragma strict
var myBulletPrefab : GameObject;
function Update ()
{
function OnTriggerEnter(other : Collision)
{
if(other.GameObject.tag === "Enemy")
{
myBulletPrefab = Instantiate();
}
}
function PosTarget(pos : Collision)
{
if (pos.GameObject.FindWithTag === "Enemy")
{
return (transform.Translate);
}
}
}
First thing I can tell you, don’t put functions inside of other functions. This will get rid of your error. Second, don’t capitalize gameObject when you’re getting the gameObject component from an object. GameObject with a capital is a type. Third, OnTriggerEnter requires a Collider type, not Collision. Forth, when you call Instantiate, you need to give an object to instantiate, a position, and rotation.
EDIT: You also should use a ‘clone’ GameObject to instantiate your prefab.
Finally, don’t use === when comparing something, use 2 equals signs, not 3 ‘==’. Try working with this, then go from there. You question is not very detailed, we need more information to help any further.
#pragma strict
var myBulletPrefab : GameObject;
var spawnPosition : Vector3;
var currentTransform : Transform;
function Update()
{
//Do not put functions inside of other functions.
//OnTriggerEnter and PosTarget are separate methods.
}
function OnTriggerEnter(other : Collider)
{
if(other.collider.gameObject.tag == "Enemy")
{
currentTransform = other.collider.transform;
spawnPosition = currentTransform.position;
var clone : GameObject = Instantiate(myBulletPrefab, spawnPosition, Quaternion.identity);
}
}