- Home /
Problem with tutorial script
im using the tutorial from the tornado twins but when i put in the script to shoot my fireball (video 5) it wont go to gameplay so i did something wrong but idk what so can someone paste the script for me.
When trying to play the game do you see a red colored error in the left-hand cornor?
That is a error message. It tells you that you have an error in one of the scripts you wrote. Post the script here and we will be able to help you further.
var speed = 3.0; var rotateSpeed = 3.0; var bullitPrefab:Transform;
function Update () { var controller : CharacterController = GetComponent(CharacterController);
// Rotate around y - axis
transform.Rotate(0, Input.GetAxis ("Horizontal") * rotateSpeed, 0);
// $$anonymous$$ove forward / backward
var forward = transform.TransformDirection(Vector3.forward);
var curSpeed = speed * Input.GetAxis ("Vertical");
controller.Simple$$anonymous$$ove(forward * curSpeed);
if(Input.GetButtonDown(''jump''))
{
var bullit = Instantiate(bullitPrefab, GameObject.Find(''spawnpoint'').transform.position, Quaternion.identity);
}
}
@script RequireComponent(CharacterController)
Answer by Kleptomaniac · Mar 03, 2012 at 01:07 AM
This code was absolutely full of errors! The compiler will not run if there are errors in a script.
You were using two quotation marks instead of one double quotation mark to define your GetButtonDown
and your GameObject.Find
. Also, a tip: don't use GameObject.Find in Update(); it is pointlessly expensive. Instead use it in either Awake() or Start() because the GameObject.Find will only need to reference that object in a variable once in order to define it. Here is the reformatted code:
var speed : float = 3.0;
var rotateSpeed : float = 3.0;
var bullitPrefab : Transform;
private var controller : CharacterController;
private var spawnPoint : GameObject;
@script RequireComponent(CharacterController)
function Start () {
controller = CharacterController = GetComponent(CharacterController);
spawnPoint = GameObject.Find("spawnpoint");
}
function Update () {
// Rotate around y - axis
transform.Rotate(0, Input.GetAxis ("Horizontal") * rotateSpeed, 0);
// Move forward / backward
var forward = transform.TransformDirection(Vector3.forward);
var curSpeed = speed * Input.GetAxis ("Vertical");
controller.SimpleMove(forward * curSpeed);
if(Input.GetButtonDown("Jump")) {
var bullit = Instantiate(bullitPrefab, spawnPoint.transform.position, Quaternion.identity);
}
}
Good luck, Klep
Edit: Code was edited due to @aldonaletto noticing some errors in my code. See comment below for info. Thanks!
You should declare the variables controller and spawnPoint outside any function (variables declared inside a function are temporary, and unknown outside). They should also be set in Start, not Awake - Awake is called during the object creation, and the spawn point object may not exist yet.
Ah O$$anonymous$$, thanks for that. Edited code to reflect this.
thanks guys but now i still get a error at the left bottom of the screen: Assets/Standard Assets/Character Controllers/Sources/Scripts/ThirdPersonController.Js(193,53): UCW0003: WARNING: Bitwise operation 'I' on boolean values won't shortcut. Did you mean 'II'?
and another one that says: Assets/moving.js(9,14): BCE0049: Expression 'CharacterController' cannot be assigned to.
I'm really new to this so I don't know much about scripting and thanks again guys.
The line 9 has a typo - it should be:
controller = GetComponent(CharacterController);
About the other error: what the hell the ThirdPersonController.js is doing in your project? Anyway, you can simply ignore this message - it's just a warning, and will not make any difference in that case.
Thank you so much I am really new to this and never scripted something before thanks for you help and everyone here thanks again now i can continue with the tutorial
Answer by aldonaletto · Mar 03, 2012 at 03:03 AM
This code has some errors and missing instructions:
- A double quote is the character ", not two single quotes!
- Most functions in Unity are case sensitive, so check if "jump" and "spawnpoint" are correctly written (the standard button name is "Jump").
- You must apply a force or set the rigidbody velocity to make the bullit move - this part of the code is missing.
The fixed code should be:
... if(Input.GetButtonDown("Jump")) // check the spelling { var bullit = Instantiate(bullitPrefab, GameObject.Find("spawnpoint").transform.position, Quaternion.identity); bullit.rigidbody.velocity = forward * 20.0; // set the bullet speed }
Your answer
Follow this Question
Related Questions
Bullet not moving 1 Answer
Problem creating a 2D scroller shooting game 2 Answers
Attaching position of an object in game to position of cursor on screen 0 Answers
No Reload Script Works 1 Answer
Disable & Re-enable Script 1 Answer