- Home /
Im getting an error ..Cannot implicitly convert type `UnityEngine.GameObject' to `BulletContrl'
//GunContrl
using UnityEngine; using System.Collections;
public class GunCntrl : MonoBehaviour {
public bool isFiring;
public BulletContrl bullet;
public float bulletSpeed;
public float timeBetweenShots;
private float shotCounter;
public Transform firePoint;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (isFiring)
{
shotCounter -= Time.deltaTime;
if(shotCounter <=5)
{
shotCounter = timeBetweenShots;
BulletContrl newBullet = (GameObject) Instantiate(bullet, firePoint.transform.position, firePoint.transform.rotation);
newBullet.speed = bulletSpeed;
}
else {
shotCounter = 5;
}
}
} }
//the BulletContrl Script (Its a separate script)using
UnityEngine; using System.Collections;
public class BulletContrl : MonoBehaviour {
public float speed;
void Start (){
}
void Update () {
transform.Translate(Vector3.up * speed * Time.deltaTime);
}
}
Answer by DungeonBrickStudios · Nov 17, 2016 at 08:08 PM
The problem is in this line here:
BulletContrl newBullet = (GameObject) Instantiate(bullet, firePoint.transform.position, firePoint.transform.rotation);
You told it you want a new BulletContrl object called newBullet on the left, but then you told it to give you a (GameObject) on the right, which is the gameobject of the bullet you wish to instantiate. So it's complaining telling you that you asked for a BulletContrl then tried to pop a GameObject into it, and that it can't just convert it to that.
What you can do is:
GameObject newBulletGameObj = (GameObject) Instantiate(bullet, firePoint.transform.position, firePoint.transform.rotation);
BulletContrl newBullet = newBulletGameObj.GetComponent<BulletContrl>();
That way, you extract the bullet script of that one bullet you just created, and then you modify the values you wish to modify. In this case, the bullet's speed.
Answer by Landern · Nov 17, 2016 at 06:17 PM
You can't convert explicitly a GameObject to a BulletContrl, case the correct type if it's of that type:
BulletContrl newBullet = (BulletContrl) Instantiate(bullet, firePoint.transform.position, firePoint.transform.rotation);
If in fact BulletContrl is a component on the GameObject, then:
GameObject newGO = (GameObject) Instantiate(bullet, firePoint.transform.position, firePoint.transform.rotation);
BulletContrl newBullet = newGO.GetComponent<BulletContrl>();
You can do it all in one line if you want.
BulletContrl newBullet = Instantiate(bullet, firePoint.transform.position, firePoint.transform.rotation).GetComponent<BulletContrl>();
Answer by jmgek · Nov 17, 2016 at 06:21 PM
You're trying to set a type of BulletContrl as a game object. In other terms, you're trying to initalize a newBullet with the BulletControl but assigning it a game object. It's the same as trying to assing a int to a string:
string myString = 22;
To fix:
BulletContrl newBullet = (GameObject) Instantiate(bullet, firePoint.transform.position, firePoint.transform.rotation)`;
Change to:
GameObject newBullet = (GameObject) Instantiate(bullet, firePoint.transform.position, firePoint.transform.rotation);
newBullet.AddComponent<BulletContrl>();
Your answer
![](https://koobas.hobune.stream/wayback/20220612100852im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Flip Player, Not the Player's Child. 3 Answers
Trying to Create a grid for a matching game 0 Answers
How to keep player in bounds? 1 Answer