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 Ngoadam123 · Apr 15, 2014 at 01:06 AM · read

Script not reading other script

I made a script that allows the player to level up their speed but it can't read the movement script. The movement script I'm using is the sample assets beta First Person Character.

 var level = 1;
 var currentStamina : float = 100.0;
 var maxStamina : int = 100;
 var regenStamina : float = 2.0;
 var drainStamina : float = 2.0;
 var currentJump : float = 1.0;
 var maxJump : float = 1.25;
 var sprintSpeed : float = 8;
 var strafeSpeed : float = 6;
 var walkSpeed : float = 4;
 
 private var playerScript : FirstPersonCharacter;
 
 function Start()
 {
     playerScript = GetComponent(FirstPersonCharacter);
 }
 
 function Update ()
 {
     //if you're moving and pressing shift then speed up take from stamina bar
     if(controller.velocity.magnitude > 0 && Input.GetKey(KeyCode.LeftShift))
     {
         currentStamina -= Time.deltaTime * drainStamina;
         playerScript.runSpeed = sprintSpeed;
     }
     
     //if not pressing anything, then normal movement speed
     else
     {
         playerScript.walkSpeed = walkSpeed;
          playerScript.strafeSpeed = walkSpeed;
     }
     
     //Stamina Regeneration
     if(playerScript.walkSpeed == walkSpeed && (currentStamina >= 0))
     {
         currentStamina += Time.deltaTime * regenStamina;
     }
     
     //if moving, pressing shift and stamina is 0 then you cannot sprint
     if(Input.GetKey(KeyCode.LeftShift) && currentStamina <= 0)
     {
         playerScript.walkSpeed = walkSpeed;
     }
     
     //if stamina regeneration set to 100, never go past
     if(currentStamina >= maxStamina)
     {
         currentStamina = maxStamina;
     }
     
     //if goes to 0, then set to 0
     if(currentStamina <= 0)
     {
         currentStamina = 0;
     }
 }
 
Comment
Add comment · Show 4
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 robertbu · Apr 15, 2014 at 01:07 AM 0
Share

Is the player script on the same game object as this script?

avatar image Ngoadam123 · Apr 17, 2014 at 10:29 PM 0
Share

Yes it is.

avatar image Ngoadam123 · Apr 17, 2014 at 10:30 PM 0
Share

Error I get is "Assets/Scripts/Player$$anonymous$$ovement - Copy.js(12,28): BCE0018: The name 'FirstPersonCharacter' does not denote a valid type ('not found'). "

avatar image AlucardJay · Apr 17, 2014 at 10:48 PM 0
Share

From memory, the beta assets are in C#. You'll have to look into Compilation Order

  • http://docs.unity3d.com/Documentation/$$anonymous$$anual/ScriptCompileOrderFolders.html

  • http://answers.unity3d.com/questions/385582/how-to-connect-2-scripts-using-c-js.html

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by SeeSharp · Apr 17, 2014 at 11:14 PM

Hey,

I think that the error might be caused because you are trying to get a C# script in a JavaScript script. Try this:

 var playerScript : MonoScript;


and you can assign it using:

 playerScript = GetComponent(YourComponent);

or if it's a C# script, you might need:

 playerScript = GetComponent("YourComponent");

in your 'Start()' function, or you could just assign it manually in the inspector.

If you need further assistance or it didn't work, feel free to ask. Oh and, sorry for any code errors. Visual Studio doesn't seem to like JavaScript.

Best of luck,

SeeSharp.

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 Ngoadam123 · Apr 18, 2014 at 06:40 PM 0
Share

Didn't work, error I got was "Assets/Scripts/Player$$anonymous$$ovement - Copy.js(16,36): BCE0022: Cannot convert 'UnityEngine.Component' to 'UnityEditor.$$anonymous$$onoScript'."

avatar image SeeSharp · Apr 21, 2014 at 03:44 PM 0
Share

Sorry for late response,

Have you tried the other method? the playerScript = GetComponent("YourComponent"); one?

I just tried it and it worked perfectly. If it still doesn't work, please P$$anonymous$$ me on the Unity Forums and I will try to help you further.

Best of luck,

SeeSharp.

avatar image
0

Answer by Regalith · Apr 17, 2014 at 11:34 PM

If one of the scripts is C# and the other is Javascript they cannot reference each other if they are in the same folder in you assets folder. This has to do with compiling order so just move all your javascript or c# scripts to standard assets and leave the other outside of it. Plugin folder also works for this

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

23 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

Related Questions

Multiple Cars not working 1 Answer

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

c# Quit button wont quit game 1 Answer

How to make it two seconds for Time.time? 2 Answers

Pause Menu background problem 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