Search Unity

  1. We are migrating the Unity Forums to Unity Discussions. On July 12, the Unity Forums will become read-only. On July 15, Unity Discussions will become read-only until July 18, when the new design and the migrated forum contents will go live. Read our full announcement for more information and let us know if you have any questions.

Resolved Getting all instances of a prefab to move

Discussion in 'Scripting' started by muppetdance, Jul 16, 2021.

  1. muppetdance


    Jul 10, 2021
    I am developing a dice rolling application that can roll multiple dice simultaneously. The app was working well when I wrote it for one dice. I then move the dice GameObject to a prefab, restructured my code accordingly and everything was working well. When I then attempted to create multiple instances of the dice, only one of the dice responds to the diceRoll() method. I feel I've done something silly with my references to the script attached to the dice GameObject, but can't quite work it out.
    Please advise what I'm missing so that all dice will roll.
    GameController (abbreviated)
    Code (CSharp):
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    6. public class GameController : MonoBehaviour
    7. {
    8.     //Dice Instantiation variables
    9.     public GameObject dicePrefab;
    10.     private List<GameObject> dice = new List<GameObject>();
    11.     private List<DiceScript> diceScripts = new List<DiceScript>();
    12.     private int numDice = 5;
    13.     Vector3 spawnLocation;
    15.     bool diceRoll;
    16.     bool diceStatic;
    17.     Vector3 diceVelocity;
    19.     // Start is called before the first frame update
    20.     void Start()
    21.     {
    22.         spawnDice();
    23.     }
    25.     // Update is called once per frame
    26.     void Update()
    27.     {
    28.         diceRoll = false;
    29.         // Detect if spacebar is pressed
    30.         if (Input.GetKeyDown(KeyCode.Space))
    31.         {
    32.             foreach (DiceScript diceScript in diceScripts)
    33.             {
    34.                 diceScript.rollDice(deviceAcceleration, acceleration);
    35.             }
    36.         }
    37.     }
    39.     void spawnDice()
    40.     {
    41.         //Instantiate creates an Object by default. Use "as GameObject"
    42.         for (int i = 0; i < numDice; i++)
    43.         {
    44.             dice.Add(Instantiate(dicePrefab, new Vector3(0, 0, 0), Quaternion.identity));
    45.             diceScripts.Add(dice[i].GetComponent<DiceScript>());
    46.         }
    47.     }
    48. }
    DiceScript (abbreviated, attached to the dice Prefab)
    Code (CSharp):
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    6. public class DiceScript : MonoBehaviour {
    8.     static Rigidbody rb;
    9.     public static Vector3 diceVelocity;
    10.     public int diceResult;
    11.     public bool diceStatic;
    13.     // Use this for initialization
    14.     void Start () {
    15.         rb = GetComponent<Rigidbody>();
    16.         rollDice(false, diceVelocity);
    17.     }
    19.     // Update is called once per frame
    20.     void Update ()
    21.     {
    22.         diceVelocity = rb.velocity;
    23.         // Check when dice has stopped moving
    24.         if (diceVelocity.x == 0f && diceVelocity.y == 0f && diceVelocity.z == 0f)
    25.         {
    26.             evaluateDice();
    27.             diceStatic = true;
    28.         }
    29.         else
    30.         {
    31.             diceStatic = false;
    32.         }
    33.     }
    35.     public void rollDice(bool deviceAcceleration, Vector3 acceleration)
    36.     {
    37.         float dirX = Random.Range(-1000, 1000);
    38.         float dirY = Random.Range(0, 1000);
    39.         float dirZ = Random.Range(-1000, 1000);
    40.         rb.AddForce(500 + dirX, 500 + dirY, 500 + dirZ, ForceMode.Force);
    41.         rb.AddTorque(dirX, dirY, dirZ);
    42.     }
    43. }
    Note that on launch, all the dice roll (this is performed within the script in the Start method).

  2. Kurt-Dekker


    Mar 16, 2013
    Everything I see above looks totally legit, at least at first glance, so it's gonna take a little bit of tracking.

    Are there the correct number of dice in your lists?

    Are you seeing any errors?

    Gotta run it and track down more about what it's actually doing vs what the code LOOKs like it should do.

    To this end, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

    Doing this should help you answer these types of questions:

    - is this code even running? which parts are running? how often does it run? what order does it run in?
    - what are the values of the variables involved? Are they initialized? Are the values reasonable?
    - are you meeting ALL the requirements to receive callbacks such as triggers / colliders (review the documentation)

    Knowing this information will help you reason about the behavior you are seeing.

    You can also put in Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene

    You could also just display various important quantities in UI Text elements to watch them change as you play the game.

    If you are running a mobile device you can also view the console output. Google for how on your particular mobile target.

    Here's an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong:
  3. muppetdance


    Jul 10, 2021
    That was quick!
    There are no errors or warnings.
    Yes, I did liberally sprinkle the code with Debug.Logs.
    I know that the Update() method is running in the DiceScript, and that the rollDice() method is being executed. I even logged a count of the iterations through the instantiated prefabs!
    Because the diceStatic is public, I can see the value changing in the inspector this is changing to false even for the dice that aren't moving, which is a little suspect and might be a clue.
    Only the value of the one dice that actually moves changes it's value (I did not include the method for evaluating the dice value). I noticed that it's the last dice (index 4) that is rolling.
  4. Kurt-Dekker


    Mar 16, 2013
    Here's your problem. This cannot be static. :)
  5. muppetdance


    Jul 10, 2021
    Yep. That was it!
    Kurt-Dekker likes this.