Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by SonicG2 · Oct 28, 2017 at 04:36 PM · targetnamespacetype

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
0

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>();
             }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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>();
         } 
     }
 }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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


screenshot-2022-03-21-083129.png (206.0 kB)
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

74 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Types and namespace not found... 1 Answer

Type or namespace 'CharacterMotor' cannot be found 1 Answer

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges