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 d3us3xmachina · Jun 27, 2013 at 02:35 PM · variablesmultiplecall

Difficulty Calling Variables from other scrtipts

Hi there, pretty new to unity here. I was wondering if anyone could help me understand what I'm doing wrong. I have a script attached to an enemy, EnemyAttack.js that looks something like this:

  #pragma strict
     
     private var Player : GameObject;
     static var battle : boolean;
     private var BattleTrigger = boolean;
     
     function Awake(){
     Player = GameObject.FindGameObjectWithTag("Player");
     }
     
     
     function OnTriggerStay (other : Collider) {
            
            if(other.gameObject == Player){
              battle = true;     
         }
         }
         
     function OnTriggerExit (other : Collider) {
     
             if(other.gameObject == Player){
              battle = false;
              
          }
          }
         
     function BattleFunction ( BattleTrigger : boolean) {
     
     }

This determines whether or not the Player is fighting someone. so then I have a script soundtriggers.js which switches the music based on the battle state, and this WORKS. As in, when the character is in range of an enemy, battle music plays, when the character leaves it stops.

 #pragma strict
 
 var musicFadeSpeed : float = 1f;
 static var battle : boolean;
 var BattleWildMusic : AudioSource;
 var TownMusic : AudioSource;
 
 function Awake(){
 
     BattleWildMusic = transform.FindChild("BattleWildMusic").audio;
     TownMusic = transform.FindChild("TownMusic").audio;
 }
 
 function Update() {
     
     BattleWildMusic.Pause();
     
     if (EnemyAttack.battle == true){
     
         TownMusic.volume = 0.0;
         BattleWildMusic.volume = 1.0;
         BattleWildMusic.Play();
         
     }
     
     else{
     
         TownMusic.volume = 1.0;
         BattleWildMusic.volume = 0.0;
         BattleWildMusic.Stop();
 
     }
     
     
     }


so now I try to access the 'battle' variable one more time with the script smoothfollow.js, and I can't for the life of me get it to work!

 var target : Transform;
 static var battle : boolean;
 // The distance in the x-z plane to the target
 var distance = 10.0;
 // the height we want the camera to be above the target
 var height = 5.0;
 // How much we 
 var heightDamping = 2.0;
 var rotationDamping = 3.0;
 
 var distanceMin = 10;
 var distanceMax = 100;
 var scrollspeed = 0.0005;
 var ySpeed = 120.0;
 var yMinLimit = 0;
 var yMaxLimit = 80;
 
 
 // Place the script in the Camera-Control group in the component menu
 @script AddComponentMenu("Camera-Control/Smooth Follow")
 
 
 function Update () {
 
     
     if (EnemyAttack.battle == true) {
     print("BATTLING");
     }
     // Early out if we don't have a target
     if (!target){
         return;
         }
     
     if (EnemyAttack.battle == false){
             
     if(Input.GetMouseButton(0)){
         height =  height + Input.GetAxis("Mouse Y") * ySpeed;
         height = Mathf.Clamp(height,yMinLimit,yMaxLimit);
         }
     
     height = Mathf.Clamp(height,yMinLimit,yMaxLimit);
         
     // Calculate the current rotation angles
     var wantedRotationAngle = target.eulerAngles.y;
     var wantedHeight = target.position.y + height;
         
     //var currentRotationAngle = transform.eulerAngles.y;
     var currentHeight = transform.position.y;
     var currentRotationAngle = transform.eulerAngles.y;
     
     distance = Mathf.Clamp(distance - Input.GetAxis("Mouse ScrollWheel")*scrollspeed, distanceMin, distanceMax);
     
     // Damp the rotation around the y-axis
     currentRotationAngle = Mathf.LerpAngle (currentRotationAngle, wantedRotationAngle, rotationDamping * Time.deltaTime);
 
     // Damp the height
     currentHeight = Mathf.Lerp (currentHeight, wantedHeight, heightDamping * Time.deltaTime);
 
     // Convert the angle into a rotation
     var currentRotation = Quaternion.Euler (0, currentRotationAngle, 0);
     
     // Set the position of the camera on the x-z plane to:
     // distance meters behind the target
     transform.position = target.position;
     transform.position -= currentRotation * Vector3.forward * distance;
 
     // Set the height of the camera
     transform.position.y = currentHeight;
     
     // Always look at the target
     transform.LookAt (target);
     }
     
     
     //if(EnemyAttack.Battle == true) {
     //transform.position = Vector3(0,0,0);
      // }
     
 }

I would really appreciate any help!

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 Drithyl · Jun 27, 2013 at 03:11 PM 0
Share

What exactly is not working when accessing the variable from the smoothfollow script? Does it give an error, or is it simply always false?

avatar image robertbu · Jun 27, 2013 at 03:23 PM 0
Share

What does 'not work' mean? It is strange that you have:

 @script AddComponent$$anonymous$$enu("Camera-Control/Smooth Follow")

But the code you have above is a modified version of the Smooth Follow script.

avatar image d3us3xmachina · Jun 27, 2013 at 05:03 PM 0
Share

So sorry gentlemen, you are both right. Won't work isn't very descriptive. I'm not at my computer, so I can't give the exact error produced right now ( I will later) , but unity won't run the code because it doesn't recognize the EnemyAttack portions of the if statements in smoothfollow.js, however when I comment out these portions of smoothfollow.js and run it, the battle variable switches back and forth just fine. Could it have something to do with the fact that the target is the player object, and it's trying to call a script from an enemy object?

avatar image d3us3xmachina · Jun 27, 2013 at 05:13 PM 0
Share

A bit more information: EnemyAttack.js is attached to the enemy object, and looks for whether or not the player has entered the enemy's detection trigger

Soundtrigger.js is attached to the game controller, which has the children BattleWild$$anonymous$$usic and Town$$anonymous$$usic

Smoothfollow.js is attached to the camera, which isn't a child of any other object. However smoothfollow.js is targeting the player character.

$$anonymous$$y overarching goal is to change the play style from third person Above behind to third person sidescroller upon entering battle. Think skyrim exploration meets smashbros combat. I simply got stuck at the part where the camera shifts. Thank you in advance for any help!

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by azmat786n · Jun 27, 2013 at 06:20 PM

hy use this

 //object1->Script1
 
 var obj2 : GameObject;
 var script2 : Script2;
 
 function Start() {
     //get object and component
     obj2 = GameObject.Find("object2");
     script2 = obj2.GetComponent(Script2);
 
     //calling methods
     //method one
     script2.sendMessage("changeName");
     //method two
     script2.changeName();
 
     //calling methods with parameters
     script2.sendMessage("changeName", Use String);
     script2.changeName(Use String);
     
     //calling return type functions
     var name = script2.getName();
 }
 
 
 
 //object2->Script2
 
 var myName : String;
 
 //simple function
 public changeName function() {
     myName = "Azmat Ali Meer";
 }
 
 //function with parameters
 public changeName function(name:String) {
     myName = name;
 }
 
 //return type function
 public getName String() {
     return "Azmat Ali Meer":
 }




Hope this help you and others :)

Comment
Add comment · Show 1 · 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 Kiloblargh · Jun 28, 2013 at 03:35 AM 0
Share

An additional tip: only use "method 1" (Send$$anonymous$$essage) in the few cases where "method 2" (dot notation) cannot be used. Send$$anonymous$$essage is slow and inelegant; comparable to repeatedly calling GameObject.Find when you could have just declared a variable.

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

17 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

Related Questions

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

How to call Variable across scripts. 2 Answers

How to use a script on multiple gameobjects, then change a variable for one of them, not the other. 3 Answers

Easily cleaning up \ deleting unused variables? 2 Answers

how to detect if a non-moving object exist in a position? 1 Answer


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