Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Checking variable conditions between seperate script files.

Discussion in 'Scripting' started by jbarrett98121, Jul 18, 2012.

  1. jbarrett98121

    jbarrett98121

    Joined:
    Jan 3, 2011
    Posts:
    251
    Hi there!

    Here is what I am wanting info on, I have two scripts, a c# script and a js script. I want to be able to check the condition of a variable from the c# cript, but call that from within the js script.

    so here's what i have going on : in the c# script there is a variable called destroyed that gets set to true when a gameobject is destroyed. what i want to do from the js script is check that that variable destroyed has been set to true, and then do something. here's what i'm thinking

    Code (csharp):
    1. if (GetComponent<AmmoScript>().destroyed = true;) {
    is this the correct way to check a variable from an outside script? AmmoScript.cs is the name of the script that has "destroyed" that i want to check on.
     
  2. jbarrett98121

    jbarrett98121

    Joined:
    Jan 3, 2011
    Posts:
    251
    hah ok that's definitely not it.....

    if (GetComponent.AmmoScript.destroyed == true)


    is closer to being correct.... but just gives me an error that it doesn't know what the heck AmmoScript is.


    how to i tell it that i am looking for a script, anyone?
     
    Last edited: Jul 18, 2012
  3. jbarrett98121

    jbarrett98121

    Joined:
    Jan 3, 2011
    Posts:
    251
    this closer still?

    Code (csharp):
    1.    
    2. var ammoScript = GetComponent<AmmoScript>();
    3.     if (ammoScript.destroyed == true){
    4. yaddayadda
    5. }
    6.  

    is it a problem that i'm trying to check a variable in a c# script from within a js script? this is proving to be a pain.
     
    Last edited: Jul 18, 2012
  4. jbarrett98121

    jbarrett98121

    Joined:
    Jan 3, 2011
    Posts:
    251
    Code (csharp):
    1. var other : AmmoScript;
    2.     other = GetComponent("AmmoScript");
    3.     if (other.destroyed == true)
    4.         {
    5.        
    6.                 Vector.DestroyLine(pathLine);
    7.             }
    this almost works..... but error of " the name AmmoScript does not denote a valid type, not found.

    close...... is anybody out there?
     
  5. DanielQuick

    DanielQuick

    Joined:
    Dec 31, 2010
    Posts:
    3,137
  6. jbarrett98121

    jbarrett98121

    Joined:
    Jan 3, 2011
    Posts:
    251
    GameObject.SendMessage? i have no idea how to use that. at all man.

    i'm looking for a little more help than that, it's obvious i have no idea what i'm doing here. so i'm looking for an example or something. a script reference.
     
  7. lwaarbroek

    lwaarbroek

    Joined:
    Oct 9, 2010
    Posts:
    19
    As far as I know C# and JS files can't read eachother so you cant use the GetComponent and need to use a SendMessage or you have to convert one of your scripts to C# or JS.

    For SendMessage you need to create a function in the ammoScript that sets destroyed to true and then use sendmessage to actually run that function. Like this:

    Code (csharp):
    1.  
    2. gameObject.SendMessage("FunctionName", functionParameter);
    3.  
    The gameObject here should be the one with the ammoScript on it.
     
  8. jbarrett98121

    jbarrett98121

    Joined:
    Jan 3, 2011
    Posts:
    251
    funtion name and function parameter would be.......... the name of the function i want to send it to..... er...?

    i just want to send the value of the variable destroyed to the other script. or read it somehow. what a frikn pain.
     
  9. DanielQuick

    DanielQuick

    Joined:
    Dec 31, 2010
    Posts:
    3,137
    Can nobody read the documentation? I even linked the exact page...

    In order for scripts to know about a script of another language, they need to be compiled in a specific order.

    If you want your .js file to know about your .cs file, you need to place the .cs file inside the "Standard Assets" or "Plugins" folder. Note that the .js file cannot be in these folders, or you will run into the same problem again.
     
  10. jbarrett98121

    jbarrett98121

    Joined:
    Jan 3, 2011
    Posts:
    251
    ok i'm another step closer with putting the c# script in standard assets. thanks. still not getting results out of it though. no errors, but no action...

    in my c# script i declare a variable "public bool destroyed"

    then later on in the same c# script the variable gets set to true.

    Code (csharp):
    1. if ((Time.time-stationaryTime) > 1.0f)
    2.             {
    3.                 destroyed = true;
    4.                 Destroy(gameObject);
    5.                
    6.             }
    in my js script i am using this to check to see if destroyed is true, and then i destroy the trajectory path that is drawn behind the ammo object

    Code (csharp):
    1.     var other : AmmoScript;
    2.     other = GetComponent("AmmoScript");
    3.     if (other.destroyed == true)
    4.         {
    5.        
    6.                 Vector.DestroyLine(pathLine);
    7.             }
    only the path does not get destroyed when the gameobject is destroyed.

    is other.destroyed checking the variable name destroyed??? i set it to true when the object is destroyed so this should be working ..... but not yet. this should be working right?


    for instance if i change the conditions like this "if (other.destroyed != true)" the line is then destroyed (before it's even drawn) so..... it's acting like destroyed does not get set to true ... although it definitely is.
     
    Last edited: Jul 19, 2012
  11. jbarrett98121

    jbarrett98121

    Joined:
    Jan 3, 2011
    Posts:
    251
    argh, so yeah when i use Debug.Log (destroyed); it does return true in the log window............

    so i'm somehow not checking that variable correctly with "if (other.destroyed == true)"


    here's the c# script that i declare the variable in, just to show what is happening in that script, and that the variable is definitely getting set.

    Code (csharp):
    1.  
    2.  
    3. using UnityEngine;
    4. using System.Collections;
    5.  
    6. public class AmmoScript : MonoBehaviour {
    7.  
    8.     private float stationaryTime;
    9.     public bool bHasBeenShot;
    10.     public bool bHasShouted;
    11.     public AudioClip boing;
    12.     public AudioClip yipee;
    13.     public AudioClip woohoo;
    14.     public int AudioFile;
    15.     public int BounceAudioFile;
    16.     public AudioClip[] BounceAudioList;
    17.     public AudioClip[] AudioList;
    18.     private AudioSource audioSource;
    19.     public bool destroyed;
    20.    
    21.    
    22.     // Use this for initialization
    23.     void Start () {
    24.         bHasBeenShot = false;
    25.         bHasShouted = false;
    26.         audioSource = GetComponent<AudioSource>();
    27.     }
    28.    
    29.     void OnCollisionEnter(Collision collision) {
    30.         audioSource.Stop();
    31.         BounceAudioFile = Random.Range(0,BounceAudioList.Length);
    32.         audio.clip = BounceAudioList[BounceAudioFile];
    33.         audio.Play ();
    34.         //audioSource.clip = boing;
    35.         //audioSource.Play();
    36.     }
    37.     // Update is called once per frame
    38.    
    39.    
    40.  
    41.  
    42. void Update () {
    43.         if (bHasBeenShot  !bHasShouted)
    44.         {
    45.             destroyed = false;
    46.             bHasShouted = true;
    47.             //audioSource.Stop();
    48.            
    49.             AudioFile = Random.Range(0,AudioList.Length);
    50.             audio.clip = AudioList[AudioFile];
    51.             audio.Play();
    52.             //audioSource.clip = (Random.value > 0.5f) ? yipee : woohoo;
    53.             //audioSource.Play();
    54.         }
    55.  
    56.         if (bHasBeenShot  rigidbody.velocity.magnitude < 0.2f)
    57.         {
    58.             // Don't update the stationary time
    59.             if ((Time.time-stationaryTime) > 1.0f)
    60.             {
    61.                 destroyed = true;
    62.                 Destroy(gameObject);
    63.                 Debug.Log (destroyed);
    64.             }
    65.         }
    66.         else
    67.         {
    68.             stationaryTime = Time.time;
    69.         }
    70.     }
    71. }
    72.  
     
  12. Pixelstudio_nl

    Pixelstudio_nl

    Joined:
    Jun 22, 2009
    Posts:
    179
    1) Define a static variable
    2) cache the reference component in the start function (so in start do the getcomponent), then get your variable
    3) if its a manager type of object create a singleton
     
  13. jbarrett98121

    jbarrett98121

    Joined:
    Jan 3, 2011
    Posts:
    251
    well, here's the script (js) that is using getcomponent to check that variable from within the cs script.......... i thought that's pretty much what i'm doing.... i don't need to bother with singletons, the object is already being instanced.. is my issue just because getcomponent is not in the start function? right now i call getcomponent in the custom function below it . shouldn't be there?

    Code (csharp):
    1. var lineMaterial : Material;
    2. var maxPoints = 500;
    3. var continuousUpdate = true;
    4. var ballPrefab : GameObject;
    5.  
    6. //var force = 16.0;
    7.  
    8. private var pathLine : VectorLine;
    9. private var pathIndex = 0;
    10. private var pathPoints : Vector3[];
    11.  
    12. function Start () {
    13.     pathPoints = new Vector3[maxPoints];
    14.     pathLine = new VectorLine("Path", pathPoints, lineMaterial, 12.0, LineType.Continuous);
    15.    
    16.     var ball = ballPrefab;
    17.     //ball.useGravity = true;
    18.     //ball.AddForce (ball.transform.forward * force, ForceMode.Impulse);
    19.    
    20.     SamplePoints (ball.transform);
    21. }
    22.  
    23. function SamplePoints (thisTransform : Transform) {
    24.     var running = true;
    25.     while (running) {
    26.         pathPoints[pathIndex] = thisTransform.position;
    27.         if (++pathIndex == maxPoints) {
    28.             running = false;
    29.         }
    30.         yield WaitForSeconds(.05);
    31.        
    32.         if (continuousUpdate) {
    33.             DrawPath();
    34.         }
    35.    
    36.     var other : AmmoScript;
    37.     other = GetComponent("AmmoScript");
    38.     if (other.isDestroyed == true)
    39.         {
    40.        
    41.                 Vector.DestroyLine(pathLine);
    42.             }
    43.    
    44.     }
    45. }
    46.  
    47. //function OnGUI () {
    48.     //if (GUI.Button(Rect(10, 10, 100, 30), "Draw Path")) {
    49.         //DrawPath();
    50.         //Vector.DestroyLine(pathLine);
    51.        
    52.     //}
    53. //}
    54.  
    55. function DrawPath () {
    56.     if (pathIndex < 2) return;
    57.     pathLine.maxDrawIndex = pathIndex-1;
    58.     Vector.DrawLine (pathLine);
    59.     Vector.SetTextureScale (pathLine, 1.0);
    60. }
     
  14. kingcharizard

    kingcharizard

    Joined:
    Jun 30, 2011
    Posts:
    1,137
    are both these scripts on the same game object?
     
  15. AngryAnt

    AngryAnt

    Keyboard Operator

    Joined:
    Oct 25, 2005
    Posts:
    3,045
    DanielQuick gave you the correct answer to your question in the very first response to this thread.

    You're not able to reference the type of the class because your JS class has not been compiled at the time your C# class is compiled. The workaround is to move the JS file to an earlier compile step - for instance by moving it into standard assets.

    You almost had it when you moved the C# file earlier, only you moved the wrong file.

    An even better answer to all of this is: Avoid mixing JS and C# in a project. While workarounds are possible, mixing will only lead to unproductive headaches.
     
  16. jbarrett98121

    jbarrett98121

    Joined:
    Jan 3, 2011
    Posts:
    251
    yes, attached to the same object king


    ant - well it's just the variable "destroyed" that's not really working (at least when checked to see if it's true), for instance i can check another variable in the c# script like.... if (bHasShouted == true){ destroy line here }

    then it does in fact destroy the line. so yes, communicating between the two is working.... just for some reason checking destroyed is not working.
     
    Last edited: Jul 19, 2012
  17. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    You're setting destroyed to true and then destroying the object itself. Therefore, if destroyed is ever true then the object is already gone and the code in your conditional wouldn't execute anyway.
     
  18. jbarrett98121

    jbarrett98121

    Joined:
    Jan 3, 2011
    Posts:
    251
    i did just fix it like so :

    if (other.bHasBeenShot rigidbody.velocity.magnitude < 0.2f) {
    destroy path here;
    }


    how could i go about adding an additional 5 second timer to that list of conditions before it's destroyed? so if (other.bHasBeenShot rigidbody.velocity.magnitude < 0.2f add an additional 5 seconds here)
     
  19. jbarrett98121

    jbarrett98121

    Joined:
    Jan 3, 2011
    Posts:
    251
    and in case you gentlemen want to see what this is all doing in action, here is a youtube vid of this trajectory feature added to my game :

    http://www.youtube.com/watch?v=B_ci47TERA4


    game is called redneck toss : angry rednecks vol. 1 :) oh yeah, i went there. hah


    highly modified version of gnome toss as you can see.
     
    Last edited: Jul 19, 2012
  20. Khyrid

    Khyrid

    Joined:
    Oct 8, 2010
    Posts:
    1,790
    Moving a script into standard assets makes it compile earlier? I never knew this, nor do I understand how to use this knowledge to my advantage.
     
  21. jbarrett98121

    jbarrett98121

    Joined:
    Jan 3, 2011
    Posts:
    251
    yeah i've never run across that in any sort of unity tutorial or anything prior to this.





    anyone know how i'd go about adding +5 second timer on the end of the conditons before the path is destroyed? then it will be perfect. (enough)
     
  22. AngryAnt

    AngryAnt

    Keyboard Operator

    Joined:
    Oct 25, 2005
    Posts:
    3,045
    It's been mentioned a few times by now - first by DanielQuick - the first respondent to this thread:

    http://docs.unity3d.com/Documentation/ScriptReference/index.Script_compilation_28Advanced29.html

    This piece of docs describes how compile order affects which types are visible to which scripts across language boundaries and how certain folders are compiled in different order. Reading it should answer a lot of questions ;)