Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 goten000023 · Dec 24, 2014 at 08:15 AM · variablenoobvalueassignartificial intelligence

cant assign variables value

i have a ai script but i cannot assign the target variable. i dont know whats wrong could someone tell me please? here is the code

 var IsPlayingOtherAnim = false;
 var Distance;
 var Player : GameObject;
 var lookAtDistance = 25.0;
 var ChaseRange = 15.0;
 var attackRange = 1.5;
 var moveSpeed = 5.0;
 var Damping = 6.0;
 var AttackRepeatTime = 1;
 static var TheDamage = 10;
 
 private var attackTime : float;
  
 var controller : CharacterController;
 var Gravity : float = 20;
 private var MoveDirection : Vector3 = Vector3.zero;
 
 
 
 
 function start () 
 {
     attackTime = Time.time;
     Player = GameObject.Find("Player");
     
 }
 
 function Update ()
 {
        
   
 
     Distance = Vector3.Distance(Player.position, transform.position);
     
     if (Distance < lookAtDistance)
     {
         lookAt();
     }
     
     if (Distance > lookAtDistance)
     {
         renderer.material.color = Color.green;
     }
     
     if (Distance < attackRange) 
     {
         Attack();
     }
     
     else if (Distance < ChaseRange)
     {
         Chase ();
     }
 }
 
 function lookAt ()
 {
     
     
     renderer.material.color = Color.yellow;
     var rotation = Quaternion.LookRotation(Player.position - transform.position);
     transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * Damping);
 }
 
 function Chase ()
 {
     
     
     renderer.material.color = Color.red;
     
     moveDirection = transform.forward;
     moveDirection *= moveSpeed;
     
     moveDirection.y -= Gravity * Time.deltaTime;
     controller.Move(moveDirection * Time.deltaTime);
     
 }
 
 function Attack ()
 
 {
     if (Time.time > attackTime) 
     {
     
     Player.SendMessage("ApplyDammage", TheDamage, SendMessageOptions.DontRequireReceiver);
     Debug.Log ("the enemy has attacked");
     attackTime = Time.time + AttackRepeatTime;
     
     }    
 }
 
 function ApplyDamage ()
 {
     ChaseRange = 30;
     moveSpeed = 4;
     lookAtDistance = 40;
 }
Comment
Add comment · Show 3
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 Mmmpies · Dec 24, 2014 at 09:54 AM 0
Share

$$anonymous$$y javascript is poor but is it looking at your player but not moving towards?

Does the enemy have a rigidbody?

If both of those are true try putting this in the Chase() function

 rigidbody.velocity = transform.forward * 10;

And see if it starts moving.

avatar image goten000023 · Dec 24, 2014 at 10:39 PM 0
Share

well i fixed that issue the Player var was originally a transform using a character controller ins$$anonymous$$d of a rigidbody using gameobject so i changed GameObject.Find("Name") to

 GameObject.Find("Name").transform

avatar image goten000023 · Dec 24, 2014 at 10:40 PM 0
Share

thanks for helping me out with this

2 Replies

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

Answer by ramonfr · Dec 24, 2014 at 08:13 PM

Did you notice that the function "Start" is called "start"? Unity will not read the Start function because of the low case letter. And it's in that function where the variable Player is assigned to a value.

Comment
Add comment · Show 2 · 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 Mmmpies · Dec 24, 2014 at 08:29 PM 0
Share

And that's why, well spotted @ramonfr

avatar image goten000023 · Dec 24, 2014 at 08:45 PM 0
Share

thankss man

avatar image
0

Answer by George_1999 · Dec 24, 2014 at 11:37 AM

Make sure the player in your scene is in fact named "Player"

Comment
Add comment · Show 14 · 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 goten000023 · Dec 24, 2014 at 07:04 PM 0
Share

it is Named "Player" it is only giving me an error that says Object reference not set to an instance of an object

avatar image Mmmpies · Dec 24, 2014 at 07:06 PM 0
Share

Named Player or tagged Player?

avatar image goten000023 · Dec 24, 2014 at 07:13 PM 0
Share

named Player

avatar image Mmmpies · Dec 24, 2014 at 07:17 PM 0
Share

Find should find it then, does it give a line number for that error and also can you run the game without "maximize on play" and upload a screen shot. Just in case we can spot something.

avatar image goten000023 · Dec 24, 2014 at 07:25 PM 0
Share

it is giving me that error only when i run the scene. because the Player variable is not assigned and the script relies on it to work properly. here is the full error i get when i run the scene.

NullReferenceException: Object reference not set to an instance of an object ZombieAI.Update () (at Assets/$$anonymous$$yScripts/ZombieAI.js:33)

Show more comments

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

Assign a random value to var personality" upon asset creation (Java) 1 Answer

assign value for all the script 2 Answers

UnassignedReferenceException: The variable enemy of EnemiesSpawner has not been assigned unity3d 0 Answers

Creating A Class Variable That Works With Any Class 0 Answers

Can't update value of var in another class 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