- Home /
gun shooting script, gun activation script if you help me solve it when i solve it i will post it here so anyone can use it
alt textI had a problem with this script of my game to shoot the pistols and I got this error error CS1525: Invalid expression term can you help me and I tried to solve it but I could not this is the script
this is the problematic script
using UnityEngine; using System.Collections;
public class Disparador : MonoBehaviour
{
Rigidbody projectile;
public int speed = 20; //Velocidad de la bala.
void Update()
{
if (Input.GetButtonDown("Fire1"))//Si se pulsa el boton izquierdo del ratón.
{
Rigidbody instantiatedProjectile = Instantiate(
projectile, transform.position, transform.rotation);
instantiatedProjectile.velocity =
transform.TransformDirection(Vector3(0, 0, speed));
Physics.IgnoreCollision(instantiatedProjectile.GetComponent.< Collider > (),
transform.root.GetComponent.< Collider > ());
}
}
}
Answer by Pokedlg3 · Apr 22, 2021 at 01:27 PM
On line 15, you must use the new Vector3(). try changing line 15 to like this:
instantiatedProjectile.velocity = transform.TransformDirection(new Vector3(0,0,speed));
This is because you need to instantiate a new class to set a new value for the vector. indicate that you are calling a constructor and not just any method that returns an object. And serves to prevent an instance with a null attribute from existing.
It turns out that you put a "." after the GetComponent, but you don't need the dot. GetComponent();
try changing like this:
Physics.IgnoreCollision(instantiatedProjectile.GetComponent< Collider > (),
transform.root.GetComponent< Collider >());
You can also write the problematic line of code, please, I don't know which vector 3 is new.
using UnityEngine; using System.Collections;
public class Disparador : MonoBehaviour { Rigidbody projectile; public int speed = 20; //Velocidad de la bala. void Update() { if (Input.GetButtonDown("Fire1"))//Si se pulsa el boton izquierdo del ratón. { Rigidbody instantiatedProjectile = Instantiate( projectile, transform.position, transform.rotation); instantiatedProjectile.velocity = instantiatedProjectile.velocity = transform.TransformDirection(new Vector3(0, 0, speed)); Physics.IgnoreCollision(instantiatedProjectile.GetComponent.< Collider > (), transform.root.GetComponent.< Collider > ()); } }
} //I still get the error and I have even more and they are error CS1525: //Invalid expression //term //error CS1001: Identifier expected
It turns out that you put a "." after the GetComponent, but you don't need the dot. GetComponent();
try changing like this:
Physics.IgnoreCollision(instantiatedProjectile.GetComponent< Collider > (),
transform.root.GetComponent< Collider >());
Your answer
Follow this Question
Related Questions
How to make gun sway script affect camera 1 Answer
Make bullet launch at center of screen 2 Answers
How to setup gun shot sound using only animator 0 Answers
Ok i got a script i need help on. 0 Answers
Need help with my gun script! 1 Answer