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 /
  • Help Room /
avatar image
0
Question by ScavengerH · Aug 30, 2016 at 09:09 PM · error messagebeginnerstuck

I'm working on a newer version of Unity, so I cant really fix this API its simple but i just cant think

Hello, So I'm a beginner and will just get straight to the point, I'm trying to make a shot go horizontally, but I don't get what my error is please help here is the code below and the error message i get.

Error Message(1) : error CS0266: Cannot implicitly convert type UnityEngine.Object' to UnityEngine.GameObject'. An explicit conversion exists (are you missing a cast?)

Error Message(2) : error CS1061: Type UnityEngine.Component' does not contain a definition for velocity' and no extension method velocity' of type UnityEngine.Component' could be found (are you missing a using directive or an assembly reference?)

using UnityEngine; using System.Collections;

public class PlayerController : MonoBehaviour { //A variable(box in inspector where you can adjust the speed of the moving character(Player)) public float speed = 5.0f; public float padding = 1; public GameObject bulletShot; private float xmax = 78; private float xmin = -79; public float projectileSpeed;

 void Start() {

 Camera camera = Camera.main;
 float distance = transform.position.z - camera.transform.position.z;
 xmin = camera.ViewportToWorldPoint(new Vector3(0,0,distance)).x + padding;
 xmax = camera.ViewportToWorldPoint(new Vector3(1,1,distance)).x + padding;
 }

 // Update is called once per frame
 void Update () {
 if (Input.GetMouseButtonDown(0)) {

 GameObject beam = Instantiate(bulletShot, transform.position, Quaternion.identity);
 beam.rigidbody2D.velocity = new Vector3 (projectileSpeed,0,0);
         }
 //Allowing the player to move in the X-axis
 if (Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.LeftArrow)) {
 transform.position = new Vector3 (
 Mathf.Clamp(transform.position.x - speed * Time.deltaTime, xmin,xmax),
 transform.position.y,
 transform.position.z
 );

 } 
  if (Input.GetKey(KeyCode.D) || Input.GetKey(KeyCode.RightArrow)) {
 // Rotate the object around its local X axis at 1 degree per second
 transform.position = new Vector3 (

 Mathf.Clamp(transform.position.x + speed* Time.deltaTime, xmin, xmax),
 transform.position.y,
 transform.position.z
 );

 }


 }

 }
Comment
Add comment
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

1 Reply

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

Answer by Landern · Aug 30, 2016 at 09:17 PM

Instantiate returns type of object(unity object), you need to cast it to a GameObject.

update line:

 GameObject beam = Instantiate(bulletShot, transform.position, Quaternion.identity);

to

 GameObject beam = (GameObject)Instantiate(bulletShot, transform.position, Quaternion.identity);

Further more you need to Get the Component type update line:

 beam.rigidbody2D.velocity = new Vector3 (projectileSpeed,0,0);

to

 beam.GetComponent<Rigidbody2D>().velocity = new Vector3 (projectileSpeed,0,0);


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 ScavengerH · Aug 30, 2016 at 09:43 PM 0
Share

Thank you very much! it worked <3 , What if later on as im working i Get an Outdated Api..... is there a way to make them automatic ? what i mean is to reset their default .....

avatar image Landern ScavengerH · Aug 30, 2016 at 09:54 PM 0
Share

No worries, in this case this hasn't changed in the UnityEngine API. Instantiate has always returned Object, etc. Furthermore Unity is pretty good about communicating changes by marking deprecated code as obsolete for a couple versions that throw a warning(Application.LoadLevel, etc) before they remove it as they introduce API to replace prior API.

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

67 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

Related Questions

can someone help me solove this error ? please, wasted 35 mins 0 Answers

Someone help me please! I am really tired 1 Answer

getting the error CS0201 and i cant fix it. 2 Answers

Animation stuck in jump whenever I play. 0 Answers

Why the Unity is not opening? 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