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( REMOVED 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

a good name would be "awesomeGameObject"

Indeed! You have an awesome flair for naming conventions.

1 Answer

1

Change ‘Start’ to ‘Update’

Nope I get this: MissingMethodException: Method not found: 'UnityEngine.GameObject.Instatiate'. I changed it back to start and now it wont work at all. Suggestions?

There's no function called "Instatiate", as the error says. Watch the typos, programming is an exact science (also make use of syntax coloring, which helps you notice typos easier). Also you need GetKeyDown, which fires once, not GetKey, which fires every frame.

My new script with this error; I thought unity had Instatiate as a component? Unknown identifier: 'Instatiate'. var force : float = 20.0f; var spawnDistance : float = 1.0f; var awesomeGameObeject : GameObject; function Update() { if(Input.GetKeyDown(KeyCode.F)) { awesomeGameObeject = Instatiate(transform.position + spawnDistance * transform.forward, transform.rotation); rigidbody.AddForce(transform.forward * force); } }

You really need to spell Instantiate correctly....

Sorry, I copy and pasted from SOMEONES COMMENT * cough * cough * alducardj. But seriously, I really should have checked my spelling when I saw that and thanks for all the help everyone! EDIT I spoke to soon: No appropriate version of 'UnityEngine.Object.Instantiate' for the argument list '(UnityEngine.Vector3, UnityEngine.Quaternion)' was found.