i know that this is a very little problem, but could you help me??
i get this error :
Assets/scripts/shoot.cs(17,50): error CS1502: The best overloaded method match for `UnityEngine.Object.Instantiate(UnityEngine.Object, UnityEngine.Vector3, UnityEngine.Quaternion)’ has some invalid arguments
with this script:
using UnityEngine;
using System.Collections;
public class shoot : MonoBehaviour {
public int max, min, reload;
public int grenades;
public int trowspeed, shootspeed;
public Transform spawnpoint;
public Transform bullet;
public Transform grenade;
void Update () {
if(Input.GetMouseButtonDown(2)){
if(min > 0){
Rigidbody fire = Instantiate(bullet , spawnpoint, Quaternion.identity)as Rigidbody;
fire.rigidbody.AddForce(transform.forward * shootspeed);
min --;
}
}
}
}
Hmm , the only other thing i can see that looks wrong is that you have created a variable of type Rigidbody fire … but than yer trying to access fire’s rigidbody , but it IS a rigidbody.
It might also jsut be a case of that you didnt assign the prefab in the editor ?
You should allways look at the error Unity shows you in the console window.
In this case i’m sure it would show an overload error and that there was a problem with the second member.
using UnityEngine;
using System.Collections;
public class shoot : MonoBehaviour {
public int max, min, reload;
public int grenades;
public int trowspeed, shootspeed;
public GameObject bullet;
public Transform grenade;
void Update () {
if(Input.GetMouseButtonDown(0)){
if(min > 0){
Rigidbody fire = Instantiate(bullet , transform.position, Quaternion.identity)as Rigidbody;
fire.AddForce(Vector3.forward * shootspeed);
min--;
}
}
}
}
i am thinking or it would work
i am not really sure any more
fun
i placed the script on the spawnpoint so i got one error fixed
I don’t see anything wrong with that last script.
If it gives a null reference then it’s the assignement that has a problem.
Make sure your bullet has a rigidbody and it’s assigned to the right slot in the inspector.
error:
InvalidCastException: Cannot cast from source type to destination type.
shoot.Update () (at Assets/scripts/shoot.cs:21)
script:
using UnityEngine;
using System.Collections;
public class shoot : MonoBehaviour {
public int max, min, reload;
public int grenades;
public int trowspeed, shootspeed;
public GameObject bullet;
public Transform grenade;
public GUIText ammo;
private Rigidbody fire;
void Update () {
ammo.text = min + "/" + max;
if(Input.GetMouseButtonDown(0)){
if(min > 0){
Rigidbody fire = (Rigidbody) Instantiate(bullet, transform.position, Quaternion.identity);
fire.AddForce(transform.forward * shootspeed);
min--;
}
}
}
}
Give your variables a capital start letter : bullet → Bullet.
The Bullet should also be a Transform type and not a GameObject
Try debugging your script.
Remove what you don’t need and see if you can find the problem.
You will keep running into these errors throughout the development of the game if you don’t understand why it’s happening.