Changing a variable from another script
Hello! I have this script for an enemy ai -
var target : Transform; //the enemy's target
static var moveSpeed : int = 100; //move speed
var rotationSpeed = 3; //speed of turning
var myTransform : Transform; //current transform data of this enemy
function Awake()
{
myTransform = transform; //cache transform data for easy access/preformance
}
function Start()
{
target = GameObject.FindWithTag("Player").transform; //target the player
}
function Update () {
//rotate to look at the player
myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime);
//move towards the player
myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
}
I'm trying to change the variable moveSpeed from another script. As you can see I've set it to be a static var, so I should be able to access it. This is the other script -
#pragma strict
function Update() {
var hit: RaycastHit; var forward = transform.TransformDirection (Vector3.forward);
var Cube : GameObject.Find("Cube");
if (Physics.Raycast (transform.position, forward, hit))
if(hit.collider.CompareTag("enemy")){
Debug.Log("Enemy Spotted!");
Cube.GetComponent.enemyai.moveSpeed = 10;
}else{
// Something blocking line of sight
}
Debug.DrawLine (transform.position, hit.point);
}
It's just a script that triggers the action (change move speed to 10) when the player looks at the enemy. However, I am getting the error "';' expected. Insert a semicolon at the end." even though you can clearly see the semicolon. The error is on line 7 which is -
var Cube : GameObject.Find("Cube");
I have been looking for hours now to try and solve this. Maybe I need to do a different method all together, but I would really appreciate some help. Thank you so much! :)
Answer by Redwolve · Apr 12, 2016 at 10:22 PM
The way I have started to handle this may be a bit different than most but hopefully it helps you out. Whenever I want anything other than the script a variable is a part of to have access to that variable I will always create a Blank Animator to use as sort of a Data Storage, usually I name it DataCanvas and dataAnimator. And I create all the variables I need for the game/program, that will be accessed throughout my game in there. Works for every number based variable and bool. and this way you just uses dataAnimator.GetInteger("Speed") or dataAnimator.SetInteger("Speed", 2)
Hopefully you can use that. like I said works for me all the time, easier than referencing script and this way everything can change it from the same spot and you can even change the default from the Inspector. Let me know if this helps.
any chance you could select my answer as the answer, trying to build my reputation up so it would help.
Your answer
Follow this Question
Related Questions
The referenced script on this Behaviour is missing - on any new script 0 Answers
AddListener fuction throwing a NullReferenceException error 1 Answer
Hai guys i need help why my scripts not running "i will make button next3dobject and prev3dobject" 1 Answer
why is this code not working 0 Answers
Help with a C# script .SetActive 1 Answer