- Home /
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;
}
}
Is the player script on the same game object as this script?
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'). "
From memory, the beta assets are in C#. You'll have to look into Compilation Order
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.
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'."
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.
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
Your answer

Follow this Question
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