Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Tyson1211 · Sep 25, 2015 at 12:51 PM · c#scripting problemerror messagetargeting

Error* Scripting problem.. Dont no how to explain I have script and photo!!

I am making my enemy AI off a few online tutorials mixed in one and I can't find out how to get rid of this error message, I havent figured out what is wrong with the coding, and I hope you can help from the picture and script..... It says;

UnassignedReferenceException: The variable target of EnemyAI has not been assigned. You probably need to assign the target variable of the EnemyAI script in the inspector. UnityEngine.Transform.get_position () (at C:/buildslave/unity/build/artifacts/generated/common/runtime/UnityEngineTransformBindings.gen.cs:20) EnemyAI.Update () (at Assets/Code/EnemyAI.cs:60)

*Don't discount this question as un informative to the person to help me, I gave the script and error, i want the error to stop showing up...alt text

 using UnityEngine;
 using System.Collections;
 
 [RequireComponent(typeof(CharacterController))]
 [RequireComponent(typeof(SphereCollider))]
 public class EnemyAI : MonoBehaviour {
     public float preceptionRadius = 5;
 
     public Transform target;
     public float movementSpeed = 5;
     public float rotationSpeed = 420;
 
     private Transform myTransform;
 
     // Use this for initialization
     void Start ()
     {
         SphereCollider sc = GetComponent<SphereCollider>();
         CharacterController cc = GetComponent<CharacterController>();
 
         if (sc == null)
             Debug.LogError("No Collider on enemy");
         else
         {
             sc.isTrigger = true;
         }
 
         if(cc == null)
         {
             Debug.LogError("There's no Character Controller");
         }
         else
         {
             sc.center = cc.center;
             sc.radius = preceptionRadius;
         }
 
         myTransform = transform;
 
         //GameObject p = GameObject.FindGameObjectWithTag("Player");
 
         //if (p == null)
         //     Debug.LogError("Could not Find the Player");
 
         //target = p.transform; 
     }
     
     // Update is called once per frame
     void Update ()
     {
         if (target)
         {
             Vector3 dir = (target.position - myTransform.position).normalized;
             float direction = Vector3.Dot(dir, transform.forward);
 
             float dist = Vector3.Distance(target.position, myTransform.position);
         }
         
         //Find or Look at Target
         myTransform.rotation = Quaternion.Slerp(myTransform.rotation, Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed * Time.deltaTime);
 
         //Movement
         myTransform.position += myTransform.forward * movementSpeed * Time.deltaTime;
     }
 
     public void OnTriggerEnter(Collider other)
     {
         Debug.Log("Entered");
         if (other.CompareTag("Player"))
         {
             target = other.transform;
         }
     }
     public void OnTriggerExit(Collider other)
     {
         Debug.Log("Exit");
         if(other.CompareTag("Player"))
         {
             target = null;
         }
     }
 }
 
error.png (245.5 kB)
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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Dave-Carlile · Sep 25, 2015 at 12:57 PM

The error message very clearly says what the problem is. The variable target doesn't have a value assigned to it and you're trying to access it. You can double click on one of the error messages in the console and it will take you to the exact line where the error occurs.

The error message says it's on line 60.... looking at that line from your script:

      myTransform.rotation = Quaternion.Slerp(myTransform.rotation, Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed * Time.deltaTime);

It is indeed referencing the target variable. If you look up a few lines you'll see that the there is a line that says if (target) {, then it it does a few things if that condition is true. That line is there because it's possible for target to not be set to anything (it's null) and you don't want to try to use it if it's not set.

So most likely line 60 should be inside of that if statement. And probably the next line as well.

Comment
Add comment · Show 1 · 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 Suddoha · Sep 25, 2015 at 01:05 PM

You've got the following in Update:

 if(target)

which is checking for null, that's correct. But afterwards you still use the target variable in the Update method without the nullcheck. (in Quaternion.LookRotation)

 myTransform.rotation = Quaternion.Slerp(myTransform.rotation, Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed * Time.deltaTime);

That's also what causes the error to happen. Transform.position is a property, properties will be kind of converted into methods, in this case to get_position(), which is the method that occurs in the error code as well.

Try to move it inside the if statement or find an alternative.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Unity Lost My Script? 1 Answer

How to unfreeze a Script in a unity5 Scene? 1 Answer

Help with basic AI script 1 Answer

count <= std::numeric_limits::max() error when viewing custom trees on terrain 0 Answers

Unloading Broken Assembly | How to solve this !? 2 Answers


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