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 BlackWingsCorp · Jun 21, 2013 at 11:52 AM · c#instantiaterigid

Instatiating a rigidbody (using C#)

hey guys what's up? I am trying to make a canon that shoots a sphere every time the player hits space. The console is showing that newball is an unexpected symbol:(just changed my code a bit but still didn't work):

 public class BallShooter : MonoBehaviour {
     public Rigidbody ball;
     public float speed = 60;
     // Use this for initialization
     void Start () {
     
     }
     
     // Update is called once per frame
     void Update () {
         Launcher();
     }
     
     IEnumerator Launcher(){
     if(Input.GetKeyDown(KeyCode.Space)){
             //rigidbody.transform.position = new Vector3(rigidbody.transform.position.x, rigidbody.transform.position.y, rigidbody.transform.position.z);
  Rigidbody newball = Instantiate(ball, transform.position, transform.rotation)
      newball.velocity = transform.TransformDirection(Vector3.forward * speed);
         
     }
 }
 }
 

 
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 BlackWingsCorp · Jun 21, 2013 at 11:57 AM 0
Share

Didn't work bro. The console displays only one error: unexpected symbol newball at the velocity line: newball.velocity = transform.forward * speed;

2 Replies

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

Answer by alexzzzz · Jun 21, 2013 at 01:41 PM

I guess the purpose of your code is to fire a ball when a player presses Space, and destroy that ball in 3 seconds.

If it's correct then this code might do the job:

 public class BallShooter : MonoBehaviour
 {
     public Rigidbody ball;
     public float speed = 60;
     public float lifetime = 3;
  
     // Update is called once per frame
     void Update ()
     {
         if(Input.GetKeyDown(KeyCode.Space))
         {
             Rigidbody newball = (Rigidbody)Instantiate(ball, transform.position, transform.rotation);
             newball.velocity = transform.forward * speed;
             Destroy(newball.gameObject, lifetime);
         }
     }
 }

Note that you have to cast the result of Instantiate(...) to the class you actually instantiate (Rigidbody in this case), and also note the semicolon at the end of that line.

Comment
Add comment · Show 1 · 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 BlackWingsCorp · Jun 21, 2013 at 02:42 PM 0
Share

So THAT's what they meant by cast!! lol 10x alot for explaining it mate.

avatar image
0

Answer by BlackWingsCorp · Jun 21, 2013 at 12:52 PM

Ok fixed it. Here's how it was supposed to be:

 using UnityEngine;
 using System.Collections;
 
 public class BallShooter : MonoBehaviour {
     public Rigidbody ball;
     public float speed = 60;
     public Rigidbody balls;
     void Start () {
     
     }
 
     void Update () {
         Launcher();
     }
     
     void Launcher(){
     if(Input.GetKeyDown(KeyCode.Space)){
             balls = Instantiate(ball, transform.position, transform.rotation)as Rigidbody;
         balls.velocity = transform.TransformDirection(Vector3.forward * speed);
     }
 }
 }
 
Comment
Add comment · 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

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

15 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

Related Questions

Check if object is destroyed on level load, if so instantiate prefab? 1 Answer

how to instantiate object with content prefab and not him self 2 Answers

Checking if object intersects? 1 Answer

how to instantiate prefab with Resources.Load ? 4 Answers

New UI Map Selection 0 Answers


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