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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by zackbradley16 · Sep 11, 2013 at 09:52 AM · androiderror

How do I get rid of these scripting Errors?

when I clicked build and run it would get half way done before saying this.

Assets/!Realistic FPS Prefab Files/Scripts/AI/AI.js(66,78): BCE0019: 'playerObj' is not a member of 'UnityEngine.Component'.

Assets/!Realistic FPS Prefab Files/Scripts/AI/AI.js(154,22): BCE0019: 'playerObj' is not a member of 'UnityEngine.Component'.

Here is the script.

 public var objectWithAnims : Transform;//the object with the Animation component automatically created by the character mesh's import settings
 private var minimumRunSpeed = 3.0;
 public var walkAnimSpeed = 1.0;
 public var runAnimSpeed = 1.0;
 var speed = 6.0;//movement speed of the NPC
 private var speedAmt = 1.0;
 var pushPower = 5.0;//physics force to apply to rigidbodies blocking NPC path
 var rotationSpeed = 5.0;
 var targetPlayer = true;//to determine if NPC should ignore player
 var shootRange = 15.0;//minimum range to target for attack
 var attackRange = 30.0;//range that NPC will start chasing target until they are within shootRange
 @HideInInspector
 var attackRangeAmt = 30.0;//increased by character damage script if NPC is damaged by player
 var sneakRangeMod : float = 0.4f;//reduce NPC's attack range by sneakRangeMod amount when player is sneaking
 private var shootAngle = 2.0;
 var dontComeCloserRange = 5.0;
 var delayShootTime = 0.35;
 private var delayShootTimeRand = 0.0;
 private var pickNextWaypointDistance = 2.0;
 @HideInInspector
 var target : Transform;
 @HideInInspector
 var playerObj : GameObject;
 private var myTransform : Transform;
 private var initMove : float;
 var doPatrol = true;
 var walkOnPatrol = true;
 public var myWaypointGroup : int;//waypoint group number that this NPC should patrol
 var searchMask : LayerMask = 0;//only layers to include in target search (for efficiency)
 var eyeHeight = 0.4;//height of rayCast starting point/origin which detects player (can be raised if NPC origin is at their feet)
 private var countBackwards : boolean = false;
 public var randomSpawnChance : float = 1.0f;//
 private var lastShot = -10.0;
 
 // Make sure there is always a character controller
 @script RequireComponent (CharacterController)
 
 function Start () {
 
     Mathf.Clamp01(randomSpawnChance);
 
     // Activate the npc based on randomSpawnChance
     if(Random.value > randomSpawnChance){
         Destroy(transform.gameObject);
     }else{
     
         //if there is no objectWithAnims defined, use the Animation Component attached to this game object
         if(objectWithAnims == null){objectWithAnims = transform;}
 
         // Set all animations to loop
         objectWithAnims.animation.wrapMode = WrapMode.Loop;
     
         // Except our action animations, Dont loop those
         objectWithAnims.animation["shoot"].wrapMode = WrapMode.Once;
         // Put idle and run in a lower layer. They will only animate if our action animations are not playing
         objectWithAnims.animation["idle"].layer = -1;
         objectWithAnims.animation["walk"].layer = -1;
         objectWithAnims.animation["run"].layer = -1;
         
         objectWithAnims.animation["walk"].speed = walkAnimSpeed;
         objectWithAnims.animation["run"].speed = runAnimSpeed;
         
         objectWithAnims.animation.Stop();
     
         //initialize AI vars
         playerObj = Camera.main.transfom.GetComponent("CameraKick").playerObj;
         attackRangeAmt = attackRange;
         initMove = Time.time;
         objectWithAnims.animation.CrossFade("idle", 0.3);
         // Auto setup player as target through tags
         if(target == null && GameObject.FindWithTag("Player") && targetPlayer){
             target = GameObject.FindWithTag("Player").transform;
         }
         if(doPatrol){
             Patrol();
         }else{
             StandWatch();
         }
         
         if(!targetPlayer){
             //ignore collisions with player if NPC is not targeting player to prevent physics oddities
             transform.gameObject.layer = 9;
         }
     }
 }
 
 function Awake () {
     myTransform = transform;
 }
 
 function StandWatch () {
     var controller : CharacterController = GetComponent(CharacterController);    
     while (true) {
     
         //play idle animation
         objectWithAnims.animation.CrossFade("idle", 0.3);
         
         //if NPC spawns in the air, move their character controller to the ground
         if(!controller.isGrounded){ 
             var down = myTransform.TransformDirection(-Vector3.up);
             controller.SimpleMove(down);
         }else{        
             if (CanSeeTarget()){
                 yield StartCoroutine("AttackPlayer");
             }
         }
         
         yield new WaitForFixedUpdate ();//could wait for longer interval than fixed update for efficiency
     }
 }
 
 function Patrol () {
     //find the next waypoint and pass our myWaypointGroup number to AutoWayPoint script
     var curWayPoint = AutoWayPoint.FindClosest(myTransform.position, myWaypointGroup);
     var controller : CharacterController = GetComponent(CharacterController);
     if(curWayPoint){//patrol if NPC has a current waypoint, otherwise stand watch
         while (true) {
             var waypointPosition = curWayPoint.transform.position;
             // Are we close to a waypoint? -> pick the next one!
             if(Vector3.Distance(waypointPosition, myTransform.position) < pickNextWaypointDistance){
                 curWayPoint = PickNextWaypoint (curWayPoint);
             }
             
             //if NPC spawns in the air, move their character controller to the ground
             if(!controller.isGrounded){ 
                 var down = myTransform.TransformDirection(-Vector3.up);
                 controller.SimpleMove(down);
             }else{    
                 // Attack the player and wait until
                 // - player is killed
                 // - player is out of sight    
                 if(target){    
                     if(CanSeeTarget()){
                         yield StartCoroutine("AttackPlayer");
                     }
                 }
             }
             //determine if NPC should walk or run on patrol
             if(walkOnPatrol){speedAmt = 1.0f;}else{speedAmt = speed;}
             // Move towards our target
             MoveTowards(waypointPosition);
             
             yield new WaitForFixedUpdate ();
         }
     }else{
         StandWatch();
         return;
     }
 }
 
 
 function CanSeeTarget () : boolean {
     var FPSWalker = playerObj.GetComponent("FPSRigidBodyWalker");
     if(FPSWalker.crouched){
         attackRangeAmt = attackRange * sneakRangeMod;//reduce NPC's attack range by sneakRangeMod amount when player is sneaking
     }else{
         attackRangeAmt = attackRange;
     }
     if(Vector3.Distance(myTransform.position, target.position) > attackRangeAmt){
         return false;
     }
     var hit : RaycastHit;
     if(Physics.Linecast (myTransform.position + myTransform.up * (1.0 + eyeHeight), target.position, hit, searchMask)){
         return hit.transform == target;
     }
     return false;
 }
 
 function Shoot () {
     // Start shoot animation
     objectWithAnims.animation.CrossFade("shoot", 0.3);
     speedAmt = 0.0f;
     //SendMessage("SetSpeed", 0.0);
     SetSpeed(0.0f);
     // Wait until half the animation has played
     yield WaitForSeconds(delayShootTime);
     // Fire gun
     BroadcastMessage("Fire");
     // Wait for the rest of the animation to finish
     yield WaitForSeconds(objectWithAnims.animation["shoot"].length - delayShootTime + Random.Range(0.0f, 0.75f));
 }
 
 function AttackPlayer () {
     var lastVisiblePlayerPosition = target.position;
     while (true) {
         if(CanSeeTarget ()){
             // Target is dead - stop hunting
             if(target == null){
                 speedAmt = 1.0f;
                 return;
             }
             // Target is too far away - give up    
             var distance = Vector3.Distance(myTransform.position, target.position);
             if(distance > attackRangeAmt){
                 speedAmt = 1.0f;
                 return;
             }
             speedAmt = speed;
             lastVisiblePlayerPosition = target.position;
             if(distance > dontComeCloserRange){
                 MoveTowards (lastVisiblePlayerPosition);
             }else{
                 RotateTowards(lastVisiblePlayerPosition);
             }
             var forward = myTransform.TransformDirection(Vector3.forward);
             var targetDirection = lastVisiblePlayerPosition - myTransform.position;
             targetDirection.y = 0;
 
             var angle = Vector3.Angle(targetDirection, forward);
 
             // Start shooting if close and player is in sight
             if(distance < shootRange && angle < shootAngle){
                 yield StartCoroutine("Shoot");
             }
         }else{
             speedAmt = speed;
             yield StartCoroutine("SearchPlayer", lastVisiblePlayerPosition);
             // Player not visible anymore - stop attacking
             if (!CanSeeTarget ()){
                 speedAmt = 1.0f;
                 return;
             }
         }
 
         yield;//dont wait any frames for smooth NPC movement while attacking player
     }
 }
 
 function SearchPlayer (position : Vector3) {
     // Run towards the player but after 3 seconds timeout and go back to Patroling
     var timeout = 3.0;
     while(timeout > 0.0){
         MoveTowards(position);
 
         // We found the player
         if(CanSeeTarget()){
             return;
         }
         timeout -= Time.deltaTime;
 
         yield;//dont wait any frames for smooth NPC movement while searching for player
     }
 }
 
 function RotateTowards (position : Vector3) {
     //SendMessage("SetSpeed", 0.0);
     SetSpeed(0.0f);
     
     var direction = position - myTransform.position;
     direction.y = 0;
     if(direction.magnitude < 0.1){
         return;
     }
     // Rotate towards the target
     myTransform.rotation = Quaternion.Slerp (myTransform.rotation, Quaternion.LookRotation(direction), rotationSpeed * Time.deltaTime * 100);
     myTransform.eulerAngles = Vector3(0, myTransform.eulerAngles.y, 0);
 }
 
 function MoveTowards (position : Vector3) {
     var direction = position - myTransform.position;
     direction.y = 0;
     if(direction.magnitude < 0.5){
         SetSpeed(0.0f);
         return;
     }
     
     // Rotate towards the target
     myTransform.rotation = Quaternion.Slerp (myTransform.rotation, Quaternion.LookRotation(direction), rotationSpeed * Time.deltaTime);
     myTransform.eulerAngles = Vector3(0, myTransform.eulerAngles.y, 0);
     // Modify speed so we slow down when we are not facing the target
     var forward = myTransform.TransformDirection(Vector3.forward);
     var speedModifier = Vector3.Dot(forward, direction.normalized);
     speedModifier = Mathf.Clamp01(speedModifier);
     // Move the character
     direction = forward * speedAmt * speedModifier;
     GetComponent (CharacterController).SimpleMove(direction);
 
     SetSpeed(speedAmt * speedModifier);
     
 }
 
 function PickNextWaypoint (currentWaypoint : AutoWayPoint) {
 
     var best = currentWaypoint;
 
     for(var cur : AutoWayPoint in currentWaypoint.connected){
 
         if(!countBackwards){
             if(currentWaypoint.waypointNumber != cur.connected.length){
                 if(currentWaypoint.waypointNumber + 1 == cur.waypointNumber){
                     best = cur;
                     break;
                 }
             }else if(currentWaypoint.waypointNumber == cur.connected.length){
                 if(currentWaypoint.waypointNumber -1 == cur.waypointNumber){
                     best = cur;
                     countBackwards = true;
                     break;
                 }
             }
         }else{
             if(currentWaypoint.waypointNumber != 1){
                 if(currentWaypoint.waypointNumber - 1 == cur.waypointNumber){
                     best = cur;
                     break;
                 }
             }else if(currentWaypoint.waypointNumber == 1){
                 if(currentWaypoint.waypointNumber + 1 == cur.waypointNumber){
                     best = cur;
                     countBackwards = false;
                     break;
                 }
             }
         
         }
         
         
     }
     
     return best;
 }
 
 //allow the NPCs to push rigidbodies in their path
 function OnControllerColliderHit (hit : ControllerColliderHit) {
     var body : Rigidbody = hit.collider.attachedRigidbody;
     // no rigidbody
     if (body == null || body.isKinematic || body.gameObject.tag == "Player")
         return;
         
     // We dont want to push objects below us
     if (hit.moveDirection.y < -0.3) 
         return;
     
     // Calculate push direction from move direction, 
     // we only push objects to the sides never up and down
     var pushDir : Vector3 = Vector3 (hit.moveDirection.x, 0, hit.moveDirection.z);
     // If you know how fast your character is trying to move,
     // then you can also multiply the push velocity by that.
     
     // Apply the push
     body.velocity = pushDir * pushPower;
 }
 
 function SetSpeed (speed : float) {
     if (speed > minimumRunSpeed){
         objectWithAnims.animation.CrossFade("run");
     }else{
         if(speed > 0){
             objectWithAnims.animation.CrossFade("walk");
         }else{
             objectWithAnims.animation.CrossFade("idle");
         }
     }
 }
Comment
Add comment · Show 1
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 vexe · Sep 11, 2013 at 11:24 AM 0
Share

Please try to narrow down your problem. Learn to debug if you don't know (if you're on $$anonymous$$onodev, or VS) - Google the problem first, fiddle around, know exactly what's going on, etc - There's a high chance that when somebody sees that code dump he will refrain /shy away from helping you - The more specific and narrowed down your problem is, the sooner you'll get your solution :)

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by cod · Sep 11, 2013 at 10:46 AM

omg such a bunch of code ... let's see, here's a useful doc on getcomponent link text

then at line 66 replace that line with

 var needScript : CameraKick;
 needScript = Camera.main.transfom.GetComponent.<CameraKick>();//note the dot after getcomponent
 playerObj = needScript.playerObj;

I hope this works :)

then try the same method for the second error

let me know if this works

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 zackbradley16 · Sep 11, 2013 at 08:48 PM

Now I receive this Error.

Assets/!Realistic FPS Prefab Files/Scripts/AI/AI.js(66,34): BCE0018: The name 'CameraKick' does not denote a valid type ('not found'). Did you mean 'System.Runtime.InteropServices.ComRegisterFunctionAttribute'?

 //initialize AI vars
         var needScript : CameraKick;
         needScript = Camera.main.transfom.GetComponent.<CameraKick>();
         //note the dot after getcomponent
         playerObj = needScript.playerObj;
         attackRangeAmt = attackRange;
         initMove = Time.time;
         objectWithAnims.animation.CrossFade("idle", 0.3);
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 vexe · Sep 12, 2013 at 02:39 AM 0
Share

Please add that as an edit to your question and not an answer.

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

17 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

Related Questions

Multiple Cars not working 1 Answer

How do I set my android app to landscape mode entirely? 1 Answer

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Script is not working at all 2 Answers

No appropriate version of 'UnityEngine.AudioSource.Play' for the argument list '(System.Type)' was found. 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