• 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 rob444 · May 23, 2015 at 02:25 PM · script error

BCE0048: Type 'UnityEngine.Component' does not support slicing.

Im getting this error with this part of the code

  if(hasAnimation) animation["walk"].speed = 2;
  if(hasAnimation) animation["run"].speed = 3;


Here is the full code

 var mainCamera : GameObject; 
 
  var mainCameraScript; 
 
  var hasAnimation : boolean; 
 
  var isMoving : boolean;
 
  private var walkSpeed : float = 1.0;
 
  private var gravity = 100.0;
 
 private var moveDirection : Vector3 = Vector3.zero;
 
 private var charController : CharacterController;
 
 
 ///////////////////////////////////////////////////////////////
 // FUNCTION :: Start ////////////////////////////////////////
 ///////////////////////////////////////////////////////////////
 
 function Start(){
  charController = GetComponent(CharacterController);
  if(hasAnimation) animation.wrapMode = WrapMode.Loop;
  mainCameraScript = mainCamera.GetComponent("controller_camera");
  
  print (mainCamera);
  print (mainCameraScript);
  
  // set animation clip speeds
  if(hasAnimation) animation["walk"].speed = 2;
  if(hasAnimation) animation["run"].speed = 3;
 // if(hasAnimation) animation["attack"].speed = 1;
 }
 
 ////////////////////////////////////////////////////////
 // FUNCTION :: Update ////////////////////////////////
 ////////////////////////////////////////////////////////
 
 function Update(){
 
  // make sure the character is in contact with the ground
  if(charController.isGrounded == true){
   
   /////////////////////////////////////
   // set which animation clip is playing //
   /////////////////////////////////////
   
   if(Input.GetButton("Escape")){
    Application.Quit();
   }
   
   // if the player is moving forwards...
   if(Input.GetAxis("Vertical") > .1 || Input.GetAxis("Vertical") < -.1 || Input.GetAxis("Horizontal") > .1 || Input.GetAxis("Horizontal") < -.1){
    
    // run or walk?
    if(Input.GetButton("Run"))
    {
     walkSpeed = 6.8;
     if(hasAnimation){ animation.CrossFade("run"); }
    } else {
     walkSpeed = 1.8;
     if(hasAnimation){ animation.CrossFade("walk"); }
    }
   
   }
   
   // if no movement, fade to idle
   else 
   {
    if(hasAnimation){ animation.CrossFade("idle"); }
   }
   
   /*if(Input.GetButton("Attack"))
      if(hasAnimation){ animation.Play("attack"); }
      else
      if(hasAnimation){ animation.CrossFade("idle"); }
   */
   ////////////////////////////////////////
   // prepare the players movement vector  //
   ////////////////////////////////////////
   
   // rotate the player based on horizontal movement requests
   var playerY = transform.eulerAngles.y;
   var cameraY = mainCamera.transform.eulerAngles.y;
   
   // default
   var playerRotY = cameraY;
   var playerMovX = 0;
   var playerMovZ = 0;
   
   // move north (relative to camera)
   if(Input.GetAxis("Vertical") > .1 && Input.GetAxis("Horizontal") > -.1 && Input.GetAxis("Horizontal") < .1){
    playerRotY = cameraY; // facing direction
    playerMovX = 0;
    playerMovZ = 1;
   }
   
   // move north east (relative to camera)
   if(Input.GetAxis("Vertical") > .1 && Input.GetAxis("Horizontal") > .1){
    playerRotY = cameraY + 45; // facing direction
    playerMovX = 0;
    playerMovZ = 1;
   }
   
   // move east (relative to camera)
   if(Input.GetAxis("Horizontal") > .1 && Input.GetAxis("Vertical") > -.1 && Input.GetAxis("Vertical") < .1){
    playerRotY = cameraY + 90; // facing direction
    playerMovX = 0;
    playerMovZ = 1;
   }
   
   // move south east (relative to camera)
   if(Input.GetAxis("Horizontal") > .1 && Input.GetAxis("Vertical") < -.1){
    playerRotY = cameraY + 135; // facing direction
    playerMovX = 0;
    playerMovZ = 1;
   }
   
   // move south (relative to camera)
   if(Input.GetAxis("Vertical") < -.1 && Input.GetAxis("Horizontal") > -.1 && Input.GetAxis("Horizontal") < .1){
    playerRotY = cameraY - 180; // facing direction
    playerMovX = 0;
    playerMovZ = 1;
   }
   
   // move south west (relative to camera)
   if(Input.GetAxis("Vertical") < -.1 && Input.GetAxis("Horizontal") < -.1){
    playerRotY = cameraY + 225; // facing direction
    playerMovX = 0;
    playerMovZ = 1;
   }
   
   // move west (relative to camera)
   if(Input.GetAxis("Horizontal") < -.1 && Input.GetAxis("Vertical") > -.1 && Input.GetAxis("Vertical") < .1){
    playerRotY = cameraY - 90; // facing direction
    playerMovX = 0;
    playerMovZ = 1;
   }
   
   // move north west (relative to camera)
   if(Input.GetAxis("Vertical") > .1 && Input.GetAxis("Horizontal") < -.1){
    playerRotY = cameraY - 45; // facing direction
    playerMovX = 0;
    playerMovZ = 1;
   }
   
   // if the character is moving, turn them to the direction they are moving in (relative to the camera)
   if(isMoving == true){
    var rotationDelay = Quaternion.Slerp(Quaternion.Euler(0, playerY, 0), Quaternion.Euler(0, playerRotY, 0), Time.deltaTime * 4);
    transform.rotation = rotationDelay;
   }
   
   // create a movement *unit* vector (relative to the players facing direction)
   moveDirection = Vector3(playerMovX,0,playerMovZ);
   
   // transform the direction vector from local space to world space
   moveDirection = transform.TransformDirection(moveDirection);
   
   // check if the character has been set into motion
   if(moveDirection == Vector3.zero){
    isMoving = false;
   } else {
    isMoving = true;
   }
  }
  
  /////////////////////////
  // transform the player  //
  /////////////////////////
  
  // push the player down with gravity (this is blocked by terrain collision though)
  moveDirection.y -= gravity * Time.deltaTime;
  
  // transform the character with the movement vector
  charController.Move(moveDirection * (Time.deltaTime * walkSpeed));
 }
 
 ////////////////////////////////////////////////////////////////////////////
 // FUNCTION :: ClampAngle ////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////////////
 
 static function ClampAngle (angle : float, min : float, max : float)
 {
     if (angle < -360)
     {
         angle += 360;
     }
  if (angle > 360)
  {
         angle -= 360;
     }
 
     return Mathf.Clamp (angle, min, max);
 } 

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

0 Replies

· Add your reply
  • Sort: 

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

Related Questions

'parent' is not a member of 'Object'. 1 Answer

Csharp script parsing error 2 Answers

my STFPSC script has errors can anyone help me? 0 Answers

Object reference not set to an instance of an object error. 1 Answer

MeshCombineUtility problem after upgrade to Unity 5? 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