- Home /
Variable type Target Unity
The script (In C#): public class Gun : MonoBehaviour { public GameObject enemy; // Use this for initialization void Start () {
}
public float damage = 100f;
// Update is called once per frame
void Update () {
if (Input.GetButtonDown("Fire1"))
{
shoot();
}
}
void shoot()
{
RaycastHit hit;
if (Physics.Raycast(transform.position,transform.forward, out hit))
{
Target targ = GetComponent<Target>();
if (enemy != null)
{
Destroy(enemy);
}
}
}
} Okay, so here is the line of code which is not working: Target targ = GetComponent(); in which I am trying to create a variable type target as I saw in this video: https://www.youtube.com/watch?v=THnivyG0Mvo However, when I tried to do this it tells me the type or namespace "Target" could not be found (are you missing a using directive or an assembly reference?) And I already tried looking for errors on this and some people say this error is caused because the script is named "Target" however, that is not the case for me as this script is labelled as Gun. Could somebody please tell me how I could fix this line of code? (Also the entire script isn't the ideal one, I plan to replace the Destroy(gameObject) with something more suitable if I could find the solution to my problem.
Answer by unidad2pete · Oct 28, 2017 at 06:18 PM
I dont see all video but I asume you have your Script on your player, and Target is the Enemy script.
In your shoot Funtion, you are saying targ is a script named Target and is on your player gameObject, but you should put the script on your enemy and acces with the script on each enemy.
using UnityEngine;
public class Gun : MonoBehaviour
{
public GameObject enemy; // Not necesary, only if you need a reference to the enemy for future actions
public float damage = 100f;
void Update()
{
if (Input.GetButtonDown("Fire1"))
{
Shoot();
}
}
void Shoot() // The are a general routine to program on all laguajes, you should name all functions with Upercase start leter
{
RaycastHit hit;
if (Physics.Raycast(transform.position, transform.forward, out hit))
{
// Target targ = GetComponent<Target>(); //
Target targ = hit.collider.GetComponent<Target>();
// Now, you have the enemy script values referenced and you can make your actions.
// I dont understand next code, your are not referenig enemy never, I mean, enemy ever is null
if (enemy != null)
{
Destroy(enemy);
}
}
}
}
Be careful, each hit search for a Target script, if the hit not have script Target you have null references. Make a layer to the raycast only for enemy gameObjects or make code to be sure the hit have script
if (hit.collider.GetComponent<Target>())
{
Target targ = hit.collider.GetComponent<Target>();
}
Answer by MrFireBall · Mar 28, 2020 at 06:59 AM
You need to change the name of your target script and replace it with "Target".
using UnityEngine;
public class GunScript : MonoBehaviour
{
public float damage = 10f;
public float range = 100f;
public Camera fpsCam;
void Update()
{
if (Input.GetButtonDown("Fire1"))
{
Shoot();
}
}
void Shoot()
{
RaycastHit hit;
if (Physics.Raycast(fpsCam.transform.position, fpsCam.transform.forward, out hit, range))
{
TargetScript target = hit.transform.GetComponent<TargetScript>();
}
}
}
Answer by unitycreator12907 · Mar 21 at 03:03 AM
Change the Enemy script name to Target
public class Target : MonoBehaviour
change the public class to Target too it is very important.
hoe this solves the problem
Your answer
Follow this Question
Related Questions
the type or namespace cannot be found 0 Answers
How I can fix This? HELP ME! 0 Answers
Error the type or namespace name 1 Answer