Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 AlexTheHollow · Jan 02, 2016 at 09:19 AM · c#unity 52dthrow

Unity 5 2d Thorw Object

I'm trying to make a simple dodgeball game. I need the player to throw a gameobject as one would do when playing Dodgeball. I don't need it to be showing or anything. Just kind of spawn as it's being thrown and play an animation.

Here's what I have so far (errors listed after code):`enter code here` public float throwDistance;

     // Use this for initialization
     void OnCollisionEnter2D (Collision2D collision) {
 
         Destroy (collision.transform.gameObject);
     
     }
     
     // Update is called once per frame
     void Update () {
     
         if (Input.GetKeyUp (KeyCode.W)) {
             var thrownItem = Instantiate(transform.position, Quaternion.identity) as Transform;
             thrownItem.Translate (0, 0, throwDistance);
             Animation.Play ("DodgeballP1Throw");
         }
 
     }

And I get these errors:

  1. Assets/Scripts/throwDodgeball.cs(19,42): error CS1501: No overload for method Instantiate' takes 2' arguments

    1. Assets/Scripts/throwDodgeball.cs(20,36): error CS1061: Type object' does not contain a definition for Translate' and no extension method Translate' of type object' could be found (are you missing a using directive or an assembly reference?)

  2. Assets/Scripts/throwDodgeball.cs(21,35): error CS0120: An object reference is required to access non-static member `UnityEngine.Animation.Play()'

I'm not a pro nor have I messed with this type of scripting so It may be a simple solution (I can figure out the animation so don't worry about that).

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 ShadyProductions · Jan 02, 2016 at 01:01 PM 0
Share

first of all, in your instantiate you also need to declare which object to instantiate.

 var thrownItem = Instantiate(yourObject, transform.position, Quaternion.identity) as Transform;

in your start function for the animation try this

 private Animation anim;
 anim = gameObject.GetComponent<Animator>();
 //if the script is attached on the gameObject that has the animator
 anim.Play("DodgeballP1Throw"); //blabla

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by AlexTheHollow · Jan 02, 2016 at 01:19 PM

Only a couple problems.

  1. Using 'private' in the start function creates MANY errors so I put it where you normally would and put the "anim = gameObject.GetComponent();" in the start function.

  2. I get this error : Assets/Scripts/throwDodgeball.cs(12,60): error CS0201: Only assignment, call, increment, decrement, and new object expressions can be used as a statement

Taking that out makes the ball appear but it is not thrown. I also tried making the Update an IEnumerator so I could have a WaitForSeconds then destroy the ball to prevent overloading. It said the Update Function cannot start a Coroutine (or something like that) and wouldn't spawn the ball anymore.

Do you know a way around that?

Any help is greatly appreciated.

Comment
Add comment · Show 3 · 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 ShadyProductions · Jan 02, 2016 at 01:28 PM 0
Share

the private I had put in there because it was self explanatory ofcourse you wouldn't put it in the start function.. and are you sure you copied the anim = gameObject.GetComponent<Animator>(); correctly? $$anonymous$$aybe using a rigidbody and adding force is better than using translate aswel?

avatar image AlexTheHollow ShadyProductions · Jan 02, 2016 at 01:52 PM 0
Share

Okay, so whatever I typed wasn't right and what you had worked.

Since I'm using seperate sprite sheets, I just used the spriteRenderer to change the sprite onkeydown.

I'm trying to get the AddForce but it's not working. I'm even looking at the Docs and they're not even working.

https://unity3d.com/learn/tutorials/modules/beginner/physics/addforce

http://docs.unity3d.com/ScriptReference/Component-rigidbody.html

It's telling me that an object reference is required. I'm working on that right now.

avatar image ShadyProductions AlexTheHollow · Jan 02, 2016 at 01:57 PM 0
Share

unity 5 changed the way you access it, You have to get its component just like the animator

 GetComponent<Rigidbody2D>().AddForce

you can also do this in the start function

 rb2d = gameObject.GetComponent<Rigidbody2D>();

and use rb2d.AddForce

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

72 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 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Button Enabling/Disabling using Collision Triggers? 1 Answer

How to Set the Game Speed Based on the Player's Current Score? 1 Answer

How to access a prefab's boolean property through collision (with the same Prefab but different Instance)? 1 Answer

Referencing variables from another script 2 Answers

C# UI list item drag onto 2d sprite in world space? 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