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 /
  • Help Room /
avatar image
0
Question by Geads · Aug 25, 2017 at 04:49 PM · 2dscripting problemscript.ai2d-sidescroller

Trying to impliment a 2D sidescroller enemeny follow player AI. Getting UnassignedReferenceException. Now idea how to fix, been lurking for days

Hello everyone. I'm currently learning Unity and I'm having trouble with this AI script. I get this error 50+ times a second while running my game.

I want the red robot to chase the grey robot.

alt text

Blockquote

UnassignedReferenceException: The variable Player of TestAI1 has not been assigned. You probably need to assign the Player variable of the TestAI1 script in the inspector. UnityEngine.Transform.get_position () (at C:/buildslave/unity/build/artifacts/generated/common/runtime/TransformBindings.gen.cs:27) TestAI1.Update () (at Assets/Scripts/TestAI1.cs:25)

Blockquote

and here is the code of my script in C#

 using System.Collections;
  using System.Collections.Generic;
  using UnityEngine;
  using UnityEngine.UI;
 
  public class TestAI1 : MonoBehaviour
  {
  
      public Transform Player;
      int MoveSpeed = 4;
      int MaxDist = 10;
      int MinDist = 5;
  
  
  
  
      void Start()
      {
  
      }
  
      void Update()
      {
          transform.LookAt(Player);
  
          if (Vector3.Distance(transform.position, Player.position) >= MinDist)
          {
  
              transform.position += transform.forward * MoveSpeed * Time.deltaTime;
  
  
  
              if (Vector3.Distance(transform.position, Player.position) <= MaxDist)
              {
                  //Call any function U want Like Shoot at here or something
              }
  
          }
      }
  }
  

If anyone can give me some insight that would be greatly appreciated!

gametesthelp1.png (153.0 kB)
Comment
Add comment · Show 2
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 WinterboltGames · Aug 25, 2017 at 06:08 PM 0
Share

this's not an answer to your question but a good tip for you...use Vector2.Distance ins$$anonymous$$d of Vector3.Distance for 2d games.

avatar image Geads WinterboltGames · Aug 25, 2017 at 06:09 PM 0
Share

Thank you, very new to all of this!

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by SuryaEU · Aug 25, 2017 at 05:57 PM

Hey, It might sound simple, but the error message looks pretty self explanatory:

You have the TestAI script on the robot chasing. You need to define what you are chasing (the "Player" object)

Maybe you just forgot to specify your object "Player", in the Unity inspector, in the TestAI script which is on the chasing robot.

You should drag your player object from the tree in your scene, and drop it in the Robot>TestAI>Player placeholder.

If you want something more visual, I found this video on youtube: https://www.youtube.com/watch?v=TnJhhz120js It's a bit more complicated than you need (here the guy is explaining how to specify several objects at once) but it should be easier to understand than plain text :)

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 Geads · Aug 25, 2017 at 06:08 PM

That is exactly what happened. I'm sorry about that. Thank you so much for your help.

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 SuryaEU · Aug 25, 2017 at 06:10 PM 0
Share

Perfect :) There is not stupid question.

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

195 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 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 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

How do I move a prefab the same speed as my scrolling background? 0 Answers

Enemy jump in unity 2d 0 Answers

Problems disabling an AI and giving control to the Player 0 Answers

Need Help Moving an object using specfic paramets 0 Answers

Script to get Eye Material to move only seems to work in Inspector/Edit Mode 0 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