Unity Community


Page 1 of 2 12 LastLast
Results 1 to 20 of 22

  1. Posts
    251

    Checking variable conditions between seperate script files.

    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:  
    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. 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 by jbarrett98121; 07-18-2012 at 02:05 PM.


  3. Posts
    251
    this closer still?

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


    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 by jbarrett98121; 07-18-2012 at 02:24 PM.


  4. Posts
    251
    Code:  
    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. 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.


  6. Location
    The Netherlands
    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:  
    1. gameObject.SendMessage("FunctionName", functionParameter);

    The gameObject here should be the one with the ammoScript on it.
    Game designer | Programmer | Lecturer

    Portfolio


  7. 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.


  8. Location
    New Jersey
    Posts
    2,146
    Quote Originally Posted by lwaarbroek View Post
    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.
    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.


  9. 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:  
    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:  
    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 by jbarrett98121; 07-19-2012 at 12:49 AM.


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


  11. Location
    netherlands
    Posts
    176
    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


  12. 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:  
    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. }


  13. Location
    PA, US
    Posts
    1,134
    are both these scripts on the same game object?

    Now I do not have skype I dont like Skype but I had to break down and start using it

  14. Keyboard operator

    Location
    Copenhagen, Denmark
    Posts
    2,929
    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.
    Emil "AngryAnt" Johansen

    Game developer, AI specialist, Unity expert
    Freelancer, ex Unity Technologies

    AI in Unity, tips and tricks: http://angryant.com, http://twitter.com/angryant

    Coding bare-footed grants you extra CPU cycles!


    Want to play? http://angryant.com/freelance/
    Behave 2 is out! http://angryant.com/2013/12/23/Behave-2-2/
    Check out ReView!


  15. 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 by jbarrett98121; 07-19-2012 at 12:53 PM.


  16. Location
    USA
    Posts
    2,538
    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.
    Fractured State - A moddable RTS game: Dev Blog | Facebook | Twitter | Youtube | ModDB

    Personal Twitter


  17. 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)


  18. 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 by jbarrett98121; 07-19-2012 at 02:22 PM.


  19. Location
    Cheonan, S. Korea
    Posts
    1,635
    Quote Originally Posted by AngryAnt View Post
    The workaround is to move the JS file to an earlier compile step - for instance by moving it into standard assets.
    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.
    Laying down the framework for my new game project now.

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •