Error accessing variable from another script

I’ve been working on this for 2 hours now can’t seem to figure out where I’m going wrong

I have 2 scripts

In one script called Move_Soldier I have a code moving the soldier and a boolean called movement = false

#pragma strict

var speed : float = 5.0;
var movement : boolean = true; 

function Start () {

}

function Update () {

GameObject.Find("soldier2").transform.Translate(Vector3(0,0,speed) * Time.deltaTime);

}

In the other script or my bullet script with all the ray cast I want to say if bullet hits trigger of soldier call movement variable from Move_Soldier script and then say movement = false and animation of soldier moving (via code) stops so instead of the soldier falling on the ground and still moving, I want him to just stay in one position.

So after I access my variable from Move_Soldier script I get this error

Assets/Models/bulletscript.js(36,20): BCE0005: Unknown identifier: ‘MoveScript’.

How can it be unknown if I already called it unless I am doing it wrong. I already had another variable from another script working so I sued the same logic but this ain’t working.

var BulletSpeed = 50; 
var speed : float = 5.0;


function Start () {

}

function Update () {

       var MoveScript : Move_Soldier = GetComponent(Move_Soldier);


        transform.Translate(Vector3.forward * Time.deltaTime * BulletSpeed);

            var hit : RaycastHit;
        if (Physics.Raycast(transform.position, transform.forward, hit))
        {
           if (hit.collider.gameObject.tag == "k") 
             GameObject.Find("soldier").animation.Play("soldierDieBack");
             

             if (hit.collider.gameObject.tag == "z") 
             GameObject.Find("soldier2").animation.Play("soldierDieBack");
             movement = false; 

                   
            }
            
                 

}

 function activation () {
               
               if (MoveScript.movement == false)

  {
    Debug.Log("enemy movement has to stop, put in the stoping code now");
  } 

}

Now the interesting thing is, if I get rid of my activation function above and copy paste the code below in the update function then it works but it gives me a null object reference error after I shoot.

  if (MoveScript.movement == false)

  {
    Debug.Log("enemy movement has to stop, put in the stoping code now");
  }

NullReferenceException: Object reference not set to an instance of an object
bulletscript.Update () (at Assets/Models/bulletscript.js:31)

Your declaring

var MoveScript : Move_Soldier = GetComponent(Move_Soldier);

inside the update function, rather than outside the update functions local variables. This means that the activation function doesn’t know what MoveScript is. Put the declartion of MoveScript with bulletSpeed and speed variables.

Thank you Duney will try tomorrow and will get back whether it works or not but what you said does make sense.

when I put it with the variables at the top I get this message

UnityException: You are not allowed to call this function when declaring a variable.
Move it to the line after without a variable declaration.
If you are using C# don’t use this function in the constructor or field initializers, Instead move initialization to the Awake or Start function.
bulletscript…ctor () (at Assets/Models/bulletscript.js:3)

Within this code

if (Physics.Raycast(transform.position, transform.forward, hit))
{

if (hit.collider.gameObject.tag == “z”)
GameObject.Find(“soldier2”).animation.Play(“soldierDieBack”);

}

where i find my gameobject could I put a special code or something that will freeze or disable all the scripts in gameobject “soldier2”???

so pseudo code

if (hit.collider.gameObject.tag == “z”)
GameObject.Find(“soldier2”).animation.Play(“soldierDieBack”).DISABLEALLCODE;

You can’t call functions like GetComponent outside a function. What you’d do is something like:

var MoveScript : Move_Soldier;

function Start () {
  Move_Soldier = GetComponent(Move_Soldier);
}