Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by mccarron81 · Jul 16, 2013 at 12:20 PM · javascriptinstantiate

instantiate not working properly (javascript)

Hi. I am working through some tutorials and getting to grips with scripting. I have created a simple script to instantiate an object to fire at a wall of blocks i have built. the problem is that as soon as i press play to test the game it is already instantiating the object before i press the mouse button(fire1). Could someone please look at the script and tell me if there is something wrong with it. Thank you.

var bullet : Rigidbody; var power : float = 1500; var moveSpeed : float = 5;

function Update (){

 var h : float = Input.GetAxis("Horizontal") * Time.deltaTime *
  moveSpeed;
 
 var v : float = Input.GetAxis("Vertical") * Time.deltaTime *
  moveSpeed;

    transform.Translate(h, v, 0);

if(Input.GetButtonUp("Fire1"));

      var instance: Rigidbody = Instantiate(bullet, transform.position,transform.rotation);
      
      var fwd: Vector3 = transform.TransformDirection(Vector3.forward);
          
          instance.AddForce(fwd * power);


}

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image amphoterik · Jul 16, 2013 at 01:00 PM 0
Share

In the future, please format your code correctly. Watch the tutorial video if you don't know how.

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by amphoterik · Jul 16, 2013 at 12:59 PM

Hello. You need to remove the semi-colin (;) from your code. This:

 if(Input.GetButtonUp("Fire1"));

Needs to be this:

 if(Input.GetButtonUp("Fire1"))

The semi colon ends the IF statement without doing anything. This is syntactically correct, but logically an error.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image mccarron81 · Jul 16, 2013 at 01:36 PM 0
Share

Thank you amphoterik works great now. i spent all morning going through answers on here and didnt even notice the semi-colon at the end. thanks again and sorry about the format. it will be done properly the next time i need to post a question.

avatar image amphoterik · Jul 16, 2013 at 01:42 PM 0
Share

Awesome. Glad it helped.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

16 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Error when trying to instantiate 1 Answer

Instanciate Object Problem for Shooting 2 Answers

How to instantiate 2D object at fixed location? 1 Answer

How to access script variables attached to a prefab at runtime in Javascript? 2 Answers

Instantiate problem 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges