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

Objects won't add to my list?

Discussion in 'Scripting' started by danmct1995, Apr 11, 2021.

  1. danmct1995

    danmct1995

    Joined:
    Apr 21, 2020
    Posts:
    70
    I have objects that I add to my list in order to stop their velocity upon a level ending. If I debug the list, Only one object reads back each time. My list is made to keep track of objects that are cloned so that I may stop their velocity upon a condition being met. The script is as follows :

    Variables (These are variables I am using the try and create my list)
    Code (CSharp):
    1.     #region MULTIBALL VARIABLES
    2.     Ball[] balls;
    3.     Ball ball;
    4.     public List<Ball> _listOfClones = new List<Ball>();
    5.     #endregion
    Function with list (The balls instantiate fine, but are not added to the list for some reason)
    Code (CSharp):
    1.     public void SpawnTwoBalls(Transform ballTransform)
    2.     {
    3.         ball = FindObjectOfType<Ball>();
    4.  
    5.         Ball ball1 = Instantiate(ball, ballTransform.position, ballTransform.rotation);
    6.         ball1.GetComponent<Rigidbody2D>().velocity = new Vector2(6,6);
    7.         Ball ball2 = Instantiate(ball, ballTransform.position, ballTransform.rotation);
    8.         ball2.GetComponent<Rigidbody2D>().velocity = new Vector2(-6,6);
    9.         _listOfClones.Add(ball1);
    10.         _listOfClones.Add(ball2);
    11.     }
    Method using list (This works fine, but only on the single object in the list from the start)


    public void StopAllClones()
    {
    foreach (Ball ball in _listOfClones)
    {
    if (ball != null)
    {
    Rigidbody2D ballBody = ball.GetComponent<Rigidbody2D>();
    ballBody.velocity = Vector2.zero;
    }
    else
    {
    Destroy(gameObject);
    }
    }
    }
     
  2. Magnesium

    Magnesium

    Joined:
    Sep 14, 2014
    Posts:
    178
    Hello,

    From a quick read, your "Destroy(gameObject)" destroys the gameObject that is currently parsing the balls. I'm not sure Unity actually destroys the object immediately before finishing it's for loop but this is probably not what you want anyway.

    I'm also confused by the line "ball = FindObjectOfType<Ball>();".

    You use the variable ball to find a Ball object currently in the scene, then you replace with a reference to a new instance.
     
    SparrowGS likes this.
  3. SparrowGS

    SparrowGS

    Joined:
    Apr 6, 2017
    Posts:
    2,536
    Calling Destroy will only destroy on the end of frame, you can call DestroyImmediate - but it should generally be avoided for reasons (I only use it on editor scripts)



    I don't understand what the else case in the StopAllClones function is trying to do.
    If the ball isn't null stop it - fine
    else Destroy(gameObject) - destroys the object this script lives on, is this what you intend?

    gameObject is a shorthand for the gameObject the monobehavior lives on.
     
    danmct1995 likes this.
  4. danmct1995

    danmct1995

    Joined:
    Apr 21, 2020
    Posts:
    70
    I've removed the Destroy line, which I see is unnecessary now and this has helped a bit. Although, the cloned objects are not being stopped still.
    The StopAllClones function is made to halt all of the gameObjects containing the "Ball" monobehavior, but is only stopping the original "Ball" gameObject. Ideally, I'm trying to keep track of every object that has this class on it and stop them when the conditions for the level completion are met.
     
  5. danmct1995

    danmct1995

    Joined:
    Apr 21, 2020
    Posts:
    70
    Without this line the gameObject that triggers the cloning of the gameObject "Ball" throws an error saying "The object you want to instantiate is null." So I was using this to give Unity a reference to my gameObject. I'm following along to a course and this section doesn't give much guidance to this area, so I'm open to any suggestions on how to do it. Even just pointing me into a direction of what to read, because I feel so stuck on this script.
     
  6. Magnesium

    Magnesium

    Joined:
    Sep 14, 2014
    Posts:
    178
    Instead of your "ball = FindObjectOfType<Ball>();", have you tried creating a new gameObject and attaching the component to it?

    Code (CSharp):
    1. GameObject ball = new GameObject();
    2. ball.AddComponent<Ball>();
    3. _listOfClones.Add(ball);
     
    danmct1995 likes this.
  7. danmct1995

    danmct1995

    Joined:
    Apr 21, 2020
    Posts:
    70
    So playing around a bit you ended up leading me to the answer. At some point I ended up creating an object to manage the list, but my gameObject that I was cloning also contained the same list. I ended up removing the script from the gameObject being cloned and it seems to work fine now. Ty for the help!