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 sinkler747 · Mar 09, 2013 at 07:46 PM · variableaccessing

when i access a variable from another script, it doesn't change the variable in that script. what am i doing wrong?

what i am trying to do, is when a enemy gets hit, it gives him a random moveSpeed value. so i accessed the variable moveSpeed from the enemyAI script. when i run the game and i shoot an enemy, it does register the change as far as the debug.Log says, but when i watch the script variables in the inspector for the enemyAI script, they are not changing. what am i doing wrong?

thanks

 #pragma strict
 
 var loweredGun: Transform;    // assign to gunMount empty game object 
 var raisedGun: Transform; // aimedGun to aimGun empty game object
 var rayOrigin: Transform;
 var gun_01: GameObject;
 var gun_02: GameObject;
 
 var myScriptMoveSpeed: enemyAI; // this is the script i am accessing, in particular the var moveSpeed variable
 
 var shootAudio: AudioClip;
 var reloadAudio: AudioClip;
 var rifleShootAudio: AudioClip;
 
 var range: float = 1000;
 var force: float = 1000;
 var clip : int   = 3;
 var bulletsPerClip:int  = 6;
 var reloadTime :float  = 3.3;
 var bulletsLeft: int = 0;
 var shootTimer: float = 0;
 var shootCooler: float = 0.9;
 
 var playerHealth: int = 100;
 
 var damage: int = 10;  
 
 var reset: boolean = false;
 //var enemyAIScript: enemyAI;
 
 private var randRange: int;
 private var randMoveSpeed: float;   // this randomizes the moveSpeed between to float values
 
 function Start () 
 {
 
     bulletsLeft = bulletsPerClip;
     gun_01.active = true;
     myScriptMoveSpeed = GameObject.FindGameObjectWithTag("Zombie").gameObject.GetComponent(enemyAI);
     //enemyAIScript = gameObject.Tag("enemy");
 }
 
 function Update () 
 {
     if(shootTimer > 0)
     {
         shootTimer -= Time.deltaTime;
     }
     
     if(shootTimer < 0)
     {
         shootTimer = 0;
     }
     
     if(Input.GetKeyDown(KeyCode.X))
     {
         SwitchWeapons();
     }
     
     if(Input.GetMouseButton(0) && bulletsLeft)
     {
         if(shootTimer == 0)
         {
             
         
         PlayShootAudio();
         RayShoot();
         
         shootTimer = shootCooler;
         
         }
         
 
     }
     
 
 
     
 }
 
 function RayShoot()
 {
     var hit: RaycastHit;
     var directionRay = rayOrigin.TransformDirection(Vector3.forward);  // make sure rayOrigin is set with a game object that will point the firing line.
     Debug.DrawRay(rayOrigin.position, directionRay * range, Color.red);
     
     if(Physics.Raycast(transform.position, directionRay, hit, range))
     {
         if(hit.collider.gameObject.name == GameObject.FindWithTag("Body").gameObject.name)
         
         {
             Debug.Log("I hit the body");
             //animation.Play("hit1");
             randRange = Random.Range(1,4);
             randMoveSpeed = Random.Range(0.01,1.0);
             Debug.Log(randRange);
             if(randRange == 1)
             {
             hit.rigidbody.animation.Play("hit1");//(Random.value(hit1, hit2));//(directionRay * force, hit.point);
             hit.collider.SendMessageUpwards("ApplyDamage", damage, SendMessageOptions.DontRequireReceiver);
             myScriptMoveSpeed.moveSpeed = 0;
             Debug.Log("movement is: ");
             Debug.Log(myScriptMoveSpeed.moveSpeed);
             yield WaitForSeconds(.3);
             hit.rigidbody.animation.Play("shamble");
             //GameObject.FindWithTag("Zombie").gameObject.GetComponent(enemyAI.moveSpeed);
             myScriptMoveSpeed.moveSpeed = randMoveSpeed;
             //myScriptMoveSpeed = GameObject.FindGameObjectWithTag("Zombie").gameObject.GetComponent(enemyAI);
             Debug.Log("movement is now: ");
             Debug.Log(myScriptMoveSpeed.moveSpeed);
             
             
             }
             else if(randRange == 2)
             {
                     hit.rigidbody.animation.Play("hit2");//(Random.value(hit1, hit2));//(directionRay * force, hit.point);
                     hit.collider.SendMessageUpwards("ApplyDamage", damage, SendMessageOptions.DontRequireReceiver);
                     myScriptMoveSpeed.moveSpeed = 0;
                     Debug.Log("movement is: ");
                     Debug.Log(myScriptMoveSpeed.moveSpeed);
                     yield WaitForSeconds(.3);
                     hit.rigidbody.animation.Play("shamble");
                     myScriptMoveSpeed.moveSpeed = randMoveSpeed;
                     Debug.Log("movement is now: ");
                     Debug.Log(myScriptMoveSpeed.moveSpeed);
             
                     
             }
             
             else if(randRange == 3)// this is the fall animation 
             {
                     hit.rigidbody.animation.Play("fallBack");//(Random.value(hit1, hit2));//(directionRay * force, hit.point);
                     hit.collider.SendMessageUpwards("ApplyDamage", damage, SendMessageOptions.DontRequireReceiver);
                     yield WaitForSeconds(.5);
 
                     myScriptMoveSpeed.moveSpeed = 0;
                     Debug.Log("movement is: ");
                     Debug.Log(myScriptMoveSpeed.moveSpeed);
                     yield WaitForSeconds(.5);
                     Debug.Log("now he stands up");
                     hit.rigidbody.animation.Play("standUp");
                     yield WaitForSeconds(.5);
                     myScriptMoveSpeed.moveSpeed = randMoveSpeed;
                     hit.rigidbody.animation.Play("shamble");
                     Debug.Log("movement is: ");
                     Debug.Log(myScriptMoveSpeed.moveSpeed);
                     
                     
             
                     
             }
             
 
         }
         
         else if(hit.collider.gameObject.name == GameObject.FindWithTag("HeadCollider").gameObject.name)
         {
             Debug.Log("That was a headshot");
             hit.rigidbody.animation.Play("fallBack");
             
             yield WaitForSeconds(1);
             hit.collider.SendMessageUpwards("KillParent",SendMessageOptions.DontRequireReceiver);
             
         }
 
     }
     
 
     bulletsLeft --;
     if(bulletsLeft <= 0)
     {
         bulletsLeft = 0;
         clip --;
     }
         
         if(bulletsLeft == 0)
         {
             Reload();
         }
     
     
 }
 
 function Reload()
 {
     PlayReloadAudio();
     yield WaitForSeconds(reloadTime);
     
     if(clip >0)
     {
         bulletsLeft = bulletsPerClip;
     }
 
     
 }
 
 function PlayShootAudio()
 {
     if(gun_01.active)
     {
     audio.PlayOneShot(shootAudio);
     }
     if(gun_02.active)
     {
         audio.PlayOneShot(rifleShootAudio);
     }
 }
 
 
 function PlayReloadAudio()
 {
     audio.PlayOneShot(reloadAudio);
 
 }
 
 function SwitchWeapons()
 {
     if (gun_01.active == true)
     {
         gun_01.SetActiveRecursively(false);
         gun_02.SetActiveRecursively(true);
     }
     
     else 
     {
         gun_01.SetActiveRecursively(true);
         gun_02.SetActiveRecursively(false);
     }
 }
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.

Follow this Question

Answers Answers and Comments

10 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

Related Questions

Is it possible to change a variable, into a script not assigned to any game object? 3 Answers

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

Another way to reference scripts? 2 Answers

Assigning varaibles to prefabs 1 Answer

Declaring Variable in Child & Access as Parent 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