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
);
}
}
}
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);
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 .....
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.