Prefab... Instantiate!

Scenario (in JS):

ballLaunch gameobject has 1) a script to instantiate a new prefab, 2) the new var prefab. Heiarchy has teh ballLaunch item and a target - in this case a simple plane with the "Finish" tag selected in the inspector.

For some reason, the prefab is not instantiating. I've gotten it to work at times, but overall it is not functioning and all my poking hasn't advanced it any.

Here is the script attached to the ballLaunch gameobject, in addition to the prefab (below):

var ball : GameObject;

private var currentBall : GameObject;
private var currentTarget : GameObject;

function Update () {
currentTarget = GameObject.FindWithTag ("Finish");
if (currentTarget == true && currentBall == false)
	ballSpawn ();

function ballSpawn () {
var currentPosition : Vector3 = transform.position;
currentBall = Instantiate (ball, currentPosition, Quaternion.identity);
currentBall.tag = "Player";

And here is the ballMoving script attached to the ball. The prefab placed in the above slot is made up of only the sphere and this script:

var ballSpeed : float = 2.0;
var ballDamping : float = 1.0;

private var currentTarget : GameObject;

function Update () {
ballMoving ();

function ballMoving () {
ballLookat = currentTarget.transform.position - transform.position;
rotation = Quaternion.LookRotation (ballLookat); 
transform.rotation = Quaternion.Slerp (transform.rotation, rotation,    Time.deltaTime * ballDamping); 
transform.Translate (Vector3.forward * Time.deltaTime * ballSpeed);


Sorry for the additional tracking additions as this is a smaller part to a larger project I am working on. Any help would be greatly appreciated.

Happy New Year to everyone!


Your problem: multiple: ballLookat in the second script needs to be: var ballLookat = blah blah blah; current target in the second script is unassigned.

put in the first script: currentBall.GetComponent("NAME OF BALL SCRIPT").currentTarget = currentTarget;

and in the second script, make the current target variable not private

One of the issues is with the true/false compare with objects. I'm not a javascript programmer but I believe the way to write this would be:

function Update () {
currentTarget = GameObject.FindWithTag ("Finish");
if (currentTarget && !currentBall)
       ballSpawn ();

After this fix it will spawn the ball, but then the second issue is the ball hasn't got currentTarget set. So this will now give exceptions.

you should compare currentBall to null. if (... && currentBall == null) i am not sure because i don't know much about java script. it will work nice in C# but i am not sure about this dynamic language.

Use Prefab in your Game Here is the tutorial for that
. Unity3D Notes: Instantiate Prefab in Your Game Tutorial .
Here is demo and code for Prefab instantiate so download and try to undestand i hope u will get the solution …