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!
Please format your code. Edit your question, select your code, and use the 101/010 button. Verify 'ballSpeed' in the Inspector. Since the variable is public, the assigned value is only used when the script is attached. Plus 200 is pretty small for a one-time application of force. Make sure the 'ballPrefab' is not set to 'isKinematic'. And make sure the projectile is not hitting anything.
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.
This was the answer I needed! This is for a school project and the manual didn't say anything about this. Thank you very much!
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;
}
As I did in the script I revamped for him.(What was posted above was just an excerpt from the larger script) Good formatting makes it easier to see little things like that,Zudegia.
Please format your code. Edit your question, select your code, and use the 101/010 button. Verify 'ballSpeed' in the Inspector. Since the variable is public, the assigned value is only used when the script is attached. Plus 200 is pretty small for a one-time application of force. Make sure the 'ballPrefab' is not set to 'isKinematic'. And make sure the projectile is not hitting anything.
– robertbuI tried to do those things, didn't help. The ball doesn't even appear. I uploaded the whole thing to my dropbox, so if anyone could check whats' wrong, I'd be very happy. https://www.dropbox.com/s/ryj9rgsjc73cxan/New%20Unity%20Project%201.rar?dl=0
– Zudegia