Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Communication between scripts?

Discussion in 'Scripting' started by Jorjor210, May 3, 2015.

  1. Jorjor210

    Jorjor210

    Joined:
    May 3, 2015
    Posts:
    91
    I am new to scripting and everything. And I am currently trying to make a game that consists of 4 turrets. I want each turret to have different shoot times at all times. But I don't want each turret to have a set shooting time. I was thinking of trying to create a script with a random number picker between (1-5). And from there I wanted to each number to represent a fire time. Then from there I would need to somehow communicate between turret scripts tht if one is shooting in 1 second then none of the other can shoot at 1 second. And so on and so forth. Does this make sense? If so is it possible? (I would loop the random number picker so tht it would switch shooting times after every shot) suggestions and tips are encouraged!
     
    Last edited: May 3, 2015
  2. steego

    steego

    Joined:
    Jul 15, 2010
    Posts:
    968
    It would probably be easier to have a script that manages all the turrets and chooses which should fire. Something like this (not tested, but should work):

    Code (csharp):
    1.  
    2. public class FireManager : MonoBehaviour
    3. {
    4.     public MyTurretScript[] myTurrets;
    5.  
    6.     void Start()
    7.     {
    8.         StartCoroutine(FireTurrets());
    9.     }
    10.  
    11.     IEnumerator FireTurrets()
    12.     {
    13.         // Select a turret
    14.         int r = Random.Range(0, myTurrets.Length);
    15.         myTurrets[r].Fire();
    16.         // Wait a random time
    17.         float t = Random.Range(1f, 5f);
    18.         yield return new WaitForSeconds(t);
    19.     }
    20. }
    21.  
    This assumes you have a MyTurretScript on your turrets, and assign them in the inspector. This script should have a Fire method to handle the actual firing.
     
  3. Jorjor210

    Jorjor210

    Joined:
    May 3, 2015
    Posts:
    91
    any chance you could retype this into JavaScript? i cant access my other js from C#
     
  4. steego

    steego

    Joined:
    Jul 15, 2010
    Posts:
    968
    I don't usually code in js, but something like this maybe:

    Code (csharp):
    1.  
    2. var myTurrets : MyTurretScript[];
    3.  
    4. function Start()
    5. {
    6.     StartCoroutine(FireTurrets());
    7. }
    8.  
    9. function FireTurrets()
    10. {
    11.     var r = Random.Range(0, myTurrets.Length);
    12.     myTurrets[r].Fire();
    13.     var t = Random.Range(1.0, 5.0);
    14.     yield WaitForSeconds(t);
    15. }
    16.  
     
  5. Jorjor210

    Jorjor210

    Joined:
    May 3, 2015
    Posts:
    91
    key getting error: "array index is out of range"

    IndexOutOfRangeException: Array index is out of range.
    TurretTimer+$FireTurrets$6+$.MoveNext () (at Assets/TurretTimer.js:11)
    UnityEngine.MonoBehaviour:StartCoroutine(IEnumerator)
    TurretTimer:Start() (at Assets/TurretTimer.js:5)

    Any idea what might be happening?
     
  6. mhgrand

    mhgrand

    Joined:
    Apr 27, 2015
    Posts:
    2
    If you have 4 turrets, to access the 4th turret, the index is 3, because the index starts with 0, not 1. So this would be:

    Code (csharp):
    1. var r =Random.Range(0, myTurrets.Length - 1)
     
  7. steego

    steego

    Joined:
    Jul 15, 2010
    Posts:
    968
    Have you assigned the turrets in the inspector?

    No, Random.Range "Returns a random integer number between min [inclusive] and max [exclusive] ", so with 4 turrets, Random.Range(0, myTurrets.Length) will return a number between 0 and 3.
     
    JoeStrout likes this.
  8. Jorjor210

    Jorjor210

    Joined:
    May 3, 2015
    Posts:
    91
    Yes I have, Still get that error.
     
  9. steego

    steego

    Joined:
    Jul 15, 2010
    Posts:
    968
    That's strange, I don't see why it shouldn't work. Just to make sure, what do you get if you print the length of myTurrets to the console? Like so:

    Code (csharp):
    1.  
    2. function FireTurrets()
    3. {
    4.     Debug.Log(myTurrets.Length);
    5.     var r = Random.Range(0, myTurrets.Length);
    6.     myTurrets[r].Fire();
    7.     var t = Random.Range(1.0, 5.0);
    8.     yield WaitForSeconds(t);
    9. }
    10.  
     
  10. Jorjor210

    Jorjor210

    Joined:
    May 3, 2015
    Posts:
    91
    blah.PNG
    Click to see full size.
    Idk why it says 0 though.
     
  11. hamsterbytedev

    hamsterbytedev

    Joined:
    Dec 9, 2014
    Posts:
    353
    Make an if statement to check if r is less than the turret array length. Should resolve it for good.
     
  12. steego

    steego

    Joined:
    Jul 15, 2010
    Posts:
    968
    Yeah that's strange. I see only see a different mistake in my script, there should be a loop inside the FireTurrets function.

    I created a test project, see the attachment. Try this in an empty project and see if you can figure it out.
     

    Attached Files:

  13. Jorjor210

    Jorjor210

    Joined:
    May 3, 2015
    Posts:
    91
    Hmm... Idk why yours is working and mine isn't. here's the code to my turret if that solves anything.

    Code (JavaScript):
    1. var LookAtTarget: Transform;
    2. var damp=6.0;
    3. var BulletPrefab: Transform;
    4. var savedTime=0;
    5. var delayTime=2f;
    6. var StartTime=3;
    7. function Update ()
    8. {
    9.     if(LookAtTarget)
    10.     {
    11.         var rotate = Quaternion.LookRotation(LookAtTarget.position - transform.position);
    12.        
    13.         var seconds: int = Time.time;
    14.        
    15.         Shoot(seconds);
    16.     }
    17. }
    18. function Shoot(seconds)
    19. {
    20.         if (seconds == savedTime)
    21.     {
    22.         var bullet = Instantiate(BulletPrefab ,transform.Find ("SpawnPoint").transform.position ,
    23.                                     Quaternion.identity);
    24.         bullet.GetComponent.<Rigidbody>().AddForce(transform.up *1000);
    25.            
    26.         savedTime=seconds+delayTime;
    27.     }
    28. }
    29.        
    30.    
    Btw. i noticed your script shoots one turret at a given interval, which is what i want. but i want it to assign a shooting time for each turret and then loop and switch.
    a good example would be that joe strout made: there's 4 turrets and 4 cards. the for cards have the times 1,2,3, and 4. you shuffle the deck and assign the cards to the turrets. turret1 gets 2 seconds, turret2 get 1 second, turret3 gets 4 seconds, turret 4 gets 3 seconds.. after the actions is complete you reshuffle the cards and re-assign them.
     
  14. steego

    steego

    Joined:
    Jul 15, 2010
    Posts:
    968
    Ok, I see what you mean, try it like this

    Code (csharp):
    1.  
    2. var myTurrets : Turret[];
    3.  
    4. function Start()
    5. {
    6.     StartCoroutine(FireTurrets());
    7. }
    8.  
    9. function FireTurrets()
    10. {
    11.     // Create a copy of the original array
    12.     var randomTurrets = new Turret[myTurrets.length];
    13.     System.Array.Copy(myTurrets, randomTurrets, myTurrets.Length);
    14.  
    15.     while (true)
    16.     {
    17.         // Randomize the array
    18.         System.Array.Sort(randomTurrets, function(t1,t2) Random.Range(-1, 2) );
    19.    
    20.        // Loop through array and fire
    21.        for (i = 0; i < randomTurrets.length; i++)
    22.        {
    23.           randomTurrets[i].Fire();
    24.            var t = Random.Range(1.0, 5.0);
    25.            yield WaitForSeconds(t);
    26.        }
    27.     }
    28. }
    29.  
    If you post the full code of your turret timer script I can try to see what could be wrong with it.
     

    Attached Files:

    Last edited: May 6, 2015