Search Unity

Passing data from one script to another

Discussion in 'Scripting' started by Hybromech, Nov 25, 2017.

  1. Hybromech

    Hybromech

    Joined:
    Nov 25, 2017
    Posts:
    11
    So my goal is simple, when the player touches a trigger box rocks fall down, what i have got actually works fine, the only issue is that only one rock object falls down and the others do not, so here is what i have.

    Gameobject (rock) using script (Rock_phys).
    Gameobject (trigger_falling) this is the trigger box using script (trigger_falling).

    I need to pass a true value to the "use gravity" parameter in the rock's rigidbody once the player meets the trigger area the code is as follows.

    code for trigger_falling:

    Code (csharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class trigger_falling : MonoBehaviour {
    7.     private int fnum = 30;
    8.     private Rock_phys rockphys;
    9.  
    10.  
    11. void Awake() //events
    12. {
    13. rockphys = GameObject.FindObjectOfType <Rock_phys> ();
    14. }
    15.           void OnTriggerEnter (Collider other)
    16.     {
    17.         rockphys.andy(fnum);
    18.         rockphys.fall(true);
    19.     }
    20.  }
    21.  
    22.  
    Code for Rock_phys:

    Code (csharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class Rock_phys : MonoBehaviour {
    7.     public Rigidbody rb;
    8.     public void andy(float f)
    9.     {
    10.         //rb.AddForce(f,0,0);  
    11.     }
    12.            public void fall(bool grav)
    13.     {
    14.         rb.useGravity = grav ;
    15.     }
    16.     void Start ()
    17.     {
    18.         rb.useGravity = false;
    19.     }
    20.     void Update () {
    21.     }
    22. }
    23.  
    Help appreciated thank you.
     
    Last edited: Apr 30, 2018
  2. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Please look at this page for posting code on the forums: https://forum.unity.com/threads/using-code-tags-properly.143875/
    You can edit your post & keep it in mind for future posts.. =)

    Okay, so my response would be: Create an array (or list) , make it public or use the attribute SerializeField.
    Then drag n drop all the rocks that would be triggered by the unique area (of falling rocks).
    This will mean instead of 1 Rock_phys you have an array/list of them. (plus, you don't need to FindObjectOfType, either).

    Lastly, just go over the list and do what you're doing now (on each element of the list or array).

    :)
     
  3. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Might look like:
    Code (csharp):
    1.  
    2. // top of the class area:
    3. [SerializeField]
    4. Rock_phys [] allrocks;
    5.  
    6. // trigger portion of code, as you had it, but with the array used:
    7. void OnTriggerEnter (Collider other)
    8. {
    9.    for(int i = 0; i < allrocks.Length; ++i) {
    10.       allrocks[i].andy(fnum);
    11.       allrocks[i].fall(true);
    12.    }
    13. }
    14. // Awake section: (erased.. gone.. :))
    15.