- Home /
Translating Object Continually
Problem:
Only translates when "F" key is held.
Question:
How can I press once and have it continue moving?
Script:
var force : float = 20.0f;
var spawnDistance : float = 1.0f;
var gameObject : GameObject;
function Start() {
if(Input.GetKey(KeyCode.F)) {
GameObject.Instatiate(transform.position + spawnDistance * transform.forward, transform.rotation);
rigidbody.AddForce(transform.forward * force);
}
}
in your current form, line 9 should be :
gameObject = Instantiate( RE$$anonymous$$OVED FOR COUGH COUGH REASONS );
but this variable name is a big no-no. Unity uses both these names as key words (gameObject and GameObject), even if you look at any Unity Scripting Reference for Instantiate or Create, they use the variable name 'go'. Basically, don't use any Unity keyword as a variable name. It's not hard to call it 'myGameObject' or simply 'go' as in the docs.
EDIT : Note the spelling error in your Instantiate also. It is up to you to proofread your code and check for spelling and syntax errors
Indeed! You have an awesome flair for na$$anonymous$$g conventions.
Answer by DaveA · Mar 18, 2013 at 10:26 PM
Change 'Start' to 'Update'
Nope I get this: $$anonymous$$issing$$anonymous$$ethodException: $$anonymous$$ethod not found: 'UnityEngine.GameObject.Instatiate'.
I changed it back to start and now it wont work at all. $$anonymous$$gestions?
There's no function called "Instatiate", as the error says. Watch the typos, program$$anonymous$$g is an exact science (also make use of syntax coloring, which helps you notice typos easier). Also you need Get$$anonymous$$eyDown, which fires once, not Get$$anonymous$$ey, which fires every frame.
$$anonymous$$y new script with this error; I thought unity had Instatiate as a component?
$$anonymous$$ identifier: 'Instatiate'.
var force : float = 20.0f;
var spawnDistance : float = 1.0f;
var awesomeGameObeject : GameObject;
function Update() {
if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.F)) {
awesomeGameObeject = Instatiate(transform.position + spawnDistance * transform.forward, transform.rotation);
rigidbody.AddForce(transform.forward * force);
}
}
You are missing the first argument of Object.Instantiate. You need a GameObject to instantiate at that position and rotation
Your answer
Follow this Question
Related Questions
My controls are inverted when facing sideways. 1 Answer
Object keeps orbiting target location in a Coroutine 0 Answers
how to move an object in 2D sense its position? 0 Answers
PLEASE help me to translate this javascript to C# for my VS10 application 1 Answer
Using Rigidbody.MovePosition With Local Position and Lerp 2 Answers