Search Unity

My references are not working properly

Discussion in 'Scripting' started by imposter_syndrom_incarnated, Sep 28, 2020.

  1. imposter_syndrom_incarnated

    imposter_syndrom_incarnated

    Joined:
    May 1, 2020
    Posts:
    51
    Hello,
    I am having this strange problem on working with a reference that connects the two classes but had no luck finding a solution yet. So I would really appreciate any help given :oops:
    I have 2 classes/scripts:
    1. Overlap class : this is the main class of the program and it has an important coroutine inside that generates balls in random positions. The balls that do not collide between each other are then added in a new list called NewlistOfObjects. Each ball is given an ID so there is also a list of IDs that is only initialized here.
    2. BallPrefab class: this is the script attached to the prefab of the ball sprite that is randomly generated, it mainly checks the collisions but also tells us with ID the ball we clicked has and each clicked ball's ID is added to the list of ID's that was initialized in the Overlap class.
    The problems:
    1. I can't work with the IDlist in the class where it was initialized, in the Overlap class, for instance I can't empty it inside Overlap, this is why I update it inside the BallPrefab that holds the reference of the Overlap class.
    2. The same reference of the Overlap class does not give me the correct total of the NewlistOfObjects. It keeps showing 0 for total despite the fact that it was updated
    This is the BallPrefab class that has the problematic reference:
    Code (CSharp):
    1. public class BallPrefab : MonoBehaviour
    2. {
    3.  
    4.     public bool collided;
    5.     public int id;
    6.     //the problematic reference
    7.     public Overlap reference;
    8.     void Start()
    9.     {
    10.         //problem: had to clear the list here and not in the class where originally instantiated
    11.         reference.IDlist.Clear();
    12.     }
    13.     private void OnMouseDown()
    14.     {
    15.         print("you clicked ball number: " + id);
    16.         reference.IDlist.Add(id);
    17.         var totID = reference.IDlist.Count;
    18.         //problem: totBalls is giving the wrong value, by showing 0, so the list is is empty based on this
    19.         var totBalls = reference.totalBalls;
    20.         print("total IDs: " + totID); //right
    21.         print("total balls: " + totBalls); //wrong
    22.     }
    23.     void OnTriggerEnter2D(Collider2D other)
    24.     {
    25.         collided = true;
    26.     }
    27.  
    28. }
    upload_2020-9-28_19-58-17.png

    Here for instance there were supposed to be 4 balls, for one collided so there are 3. I clicked o a ball and you can see that the total balls are shown to be 0, despite having been updated to being 3 in the Overlap class.

    Update: Not sure why but if I change the way of referencing the problem number 2 is solved. Instead of using the reference field called 'reference' in the way above, I used FindObjectOfType<Overlap>(). I don't know why this works the the above method doesn't and I still don't know why the list can't be cleared in the class where I instantiated it

    Any help or recommendations will be much appreciated :):):)
    -------------------------------------------------------------------------------
    PS: for those interested in checking the Overlap class as well:
    Code (CSharp):
    1. public class Overlap : MonoBehaviour
    2. {
    3.     private float xRandomPos, yRandomPos;
    4.     Vector3 finalRandomPos;
    5.     public int numToGenerate;
    6.  
    7.     public List<GameObject> listOfObjects;
    8.     public List<GameObject> NewlistOfObjects;
    9.     //problematic line
    10.     public int totalBalls { get; set; }
    11.     public List<int> IDlist;
    12.  
    13.  
    14.     public GameObject background;
    15.     MeshCollider borders;
    16.  
    17.     void Start()
    18.     {
    19.         borders = background.GetComponent<MeshCollider>();
    20.         WrappedCoroutine();
    21.     }
    22.  
    23.     /// <summary>
    24.     /// For the Button to click
    25.     /// </summary>
    26.     public void WrappedCoroutine()
    27.     {
    28.         StartCoroutine(Coroutine());
    29.     }
    30.  
    31.     /// <summary>
    32.     /// Main method
    33.     /// </summary>
    34.     /// <returns></returns>
    35.     public IEnumerator Coroutine()
    36.     {
    37.         //coroutine
    38.         WaitForSeconds wait = new WaitForSeconds(1f);
    39.  
    40.         int randomItemFromListIndex;
    41.         GameObject randomItemFromList;
    42.  
    43.         int idNum = 1;
    44.         for (int i = 0; i < numToGenerate; i++)
    45.         {
    46.             //where to generate
    47.             xRandomPos = Random.Range(borders.bounds.min.x, borders.bounds.max.x);
    48.             yRandomPos = Random.Range(borders.bounds.min.y, borders.bounds.max.y);
    49.             finalRandomPos = new Vector3(xRandomPos, yRandomPos, 0f);
    50.          
    51.             //what to generate
    52.             randomItemFromListIndex = Random.Range(0, listOfObjects.Count);
    53.             randomItemFromList = listOfObjects[randomItemFromListIndex];
    54.          
    55.             //to detect collision
    56.  
    57.             GameObject ball = Instantiate(randomItemFromList, finalRandomPos, Quaternion.identity);
    58.  
    59.             ball.GetComponent<Renderer>().enabled = false;
    60.          
    61.             yield return new WaitForFixedUpdate();
    62.          
    63.          
    64.             bool hasCollided = ball.GetComponent<BallPrefab>().collided;
    65.          
    66.             print("collided? " + hasCollided);
    67.             if (hasCollided == false)
    68.             {
    69.                 ball.GetComponent<BallPrefab>().id = idNum++;
    70.                 NewlistOfObjects.Add(ball);
    71.             }
    72.         }
    73.         totalBalls = NewlistOfObjects.Count;
    74.         print("total balls minus those collided: " + totalBalls);
    75.  
    76.         //loop to make them appear on screen and check the IDs
    77.         for (int i = 0; i < totalBalls; i++)
    78.         {
    79.             NewlistOfObjects[i].GetComponent<Renderer>().enabled = true;
    80.             print("ID of the ball " + i + " is " + NewlistOfObjects[i].GetComponent<BallPrefab>().id);
    81.             yield return wait;
    82.         }
    83.     }
    84.  
    85.     /// <summary>
    86.     /// Restarts the current scene when the button is clicked
    87.     /// </summary>
    88.     public void Restart()
    89.     {
    90.         SceneManager.LoadScene("Overlap");
    91.     }
    92.  
    93. }

     
    Last edited: Sep 28, 2020