Shooting a ball by cloning it

I have a problem. I can’t get my script to do anything in-game, even though I have everything set. Nothing happens when I press space. Can anyone help me, please?

var ballPrefab:Transform;
var ballSpeed:float = 200; 
function Update () {
	if(Input.GetKeyDown(KeyCode.Space)){
    var ball :Transform= Instantiate(ballPrefab,
        transform.Find("SpawnPoint").transform.position,
        Quaternion.identity);
    ball.rigidbody.AddForce(transform.forward*ballSpeed); 
}

Answer: The ball wasn’t set in the hierarchy “player” Ball Prefab. I thought it was enough to set it on the assets tab, straight on the script but it seems that’s useless. Guess my manual isn’t very clear. Thank you everyone!

I believe your error can be fixed easily. I downloaded you project and selecting the player object in the Hierachy panel reveals that the scrip component has a field named ballPrefab. With nothing assigned. What you do is :

Select the Player object in the hierachy panel.

On the asset explorer navigate to the folder that contains the ball.

Drag the ball to the field in the script.

We need more context. Check to make sure your behaviour is assigned to a gameobject in your scene, and that there are no errors occurring.

I made some additions, but in the future, please format your code like I show in the new version.

It’s easier to read, and debug.

Drop the ball prefab in the inspector, and the spawnpoint as well.

Here ya go:

#pragma strict

var speed = 5.0f;
var rotateSpeed:float ;
var sphere:GameObject;
var child:Transform;
var ballPrefab:Rigidbody;
var ballSpeed:float = 200;
var spawnPoint:Transform; 

function Update () {

	if(Input.GetKeyDown(KeyCode.Space)){
		
		var ball = Instantiate(ballPrefab, spawnPoint.position,   spawnPoint.rotation) as Rigidbody;
		ball.rotation = Quaternion.LookRotation(ball.velocity);
    	ball.rigidbody.AddForce(transform.forward*ballSpeed); 
	}
	
    transform.Translate(Vector3(0, 0, Input.GetAxis("Vertical")) * Time.deltaTime * speed);
    transform.Rotate(Vector3(0, Input.GetAxis("Horizontal"), 0) * rotateSpeed * Time.deltaTime);
    
    if(Input.GetKeyDown(KeyCode.E)){
    
        sphere = GameObject.CreatePrimitive(PrimitiveType.Sphere);
        sphere.transform.position = transform.position;
        sphere.transform.position.y = transform.position.y+4;
        sphere.transform.parent = transform;
    }
    
    if(Input.GetKeyDown(KeyCode.Q)){
    
        sphere.transform.parent = null;
    }
       //….
    if(Input.GetKeyDown(KeyCode.R)){
    
        child = transform.Find("Head");
        child.renderer.material.color = Color.red;
    } 
    
    else if(Input.GetKeyDown(KeyCode.B)){
    
        child = transform.Find("Head");
        child.renderer.material.color = Color.blue;
    }
    
    else if(Input.GetKeyDown(KeyCode.Y)){
    
        child = transform.Find("Head");
        child.renderer.material.color = Color.yellow;
    }
    
    if(Input.GetKeyDown(KeyCode.R)){
        
        	if(!rigidbody){
        	
            	gameObject.AddComponent(Rigidbody);
            	rigidbody.constraints = RigidbodyConstraints.FreezeRotationX |
                RigidbodyConstraints.FreezeRotationZ;
                
        		}
    		}
    	}
    	
function FixedUpdate () {

    if(rigidbody){
    
        rigidbody.AddForce(Vector3(0,0,Input.GetAxis("Vertical"))*speed); 
        rigidbody.AddTorque(Vector3(0,Input.GetAxis("Horizontal"),0)*rotateSpeed); 
    } 
}

If you except this answer click the checkmark on the left, and hook a brother up with a thumbs up.:smiley:

You are missing a closing bracket for your if statement, and it should be

var ball : Transform = Instantiate(ballPrefab, GameObject.Find("SpawnPoint").transform.position, Quaternion.identity);

instead of transform.Find.

However, if you are going to be pressing the spacebar often, then it would be best to set SpawnPoint as a variable, instead of finding it every time.

var spawnPoint : Transform;
function Start () {
	spawnPoint = GameObject.Find("SpawnPoint").transform;
}