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

Question How to access an array of GameObjects and tell it to rotate

Discussion in 'Scripting' started by julien83franceschi, Jun 26, 2023.

  1. julien83franceschi

    julien83franceschi

    Joined:
    Nov 14, 2020
    Posts:
    178
    Hello everyone,

    I declare a GameObjects array variable (public static GameObject [] prefabs) that I place in a Main file.

    The problem is that I've created another file (rotationRight.cs) in which I place the following code:

    Code (CSharp):
    1. void OnMouseDown()
    2.         {
    3.           Main.prefabs[0].transform.RotateAround(Vector3.zero, Vector3.up, 35);
    4.         }
    I'd like one of the pentominoes (the first: of index[0]) to rotate (with transform.RotateAround) when a capsule is clicked (see code above).

    But nothing rotates.

    Thanks for your help,

    A+
     
  2. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    3,043
    And how do you know that this gets called?
     
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,563
    Why are you rotating the prefabs??? Don't you want to rotate what is actually IN the scene, aka, the instances?

    If
    Main.prefabs[]
    is actually the instances and not the prefabs, do yourself a favor and rename the variables!!! There's enough confusion in this world.

    If you are making a pentomino / tetris style game, rotating stuff like this is still going to require all the work to figure out where it fits in the grid.

    You may wish to just work through a few of the thousands of tutorials on tracking grid-based tiles, clearing tiles, etc.

    Otherwise, as Orion says, is any of this code even executing? Find out this way:

    Time to start debugging! Here is how you can begin your exciting new debugging adventures:

    You must find a way to get the information you need in order to reason about what the problem is.

    Once you understand what the problem is, you may begin to reason about a solution to the problem.

    What is often happening in these cases is one of the following:

    - the code you think is executing is not actually executing at all
    - the code is executing far EARLIER or LATER than you think
    - the code is executing far LESS OFTEN than you think
    - the code is executing far MORE OFTEN than you think
    - the code is executing on another GameObject than you think it is
    - you're getting an error or warning and you haven't noticed it in the console window

    To help gain more insight into your problem, 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 names of the GameObjects or Components involved?
    - 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 supply a second argument to Debug.Log() and when you click the message, it will highlight the object in scene, such as
    Debug.Log("Problem!",this);


    If your problem would benefit from in-scene or in-game visualization, Debug.DrawRay() or Debug.DrawLine() can help you visualize things like rays (used in raycasting) or distances.

    You can also call Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene manually, looking for all the parts, where they are, what scripts are on them, etc.

    You can also call GameObject.CreatePrimitive() to emplace debug-marker-ish objects in the scene at runtime.

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

    Visit Google for how to see console output from builds. If you are running a mobile device you can also view the console output. Google for how on your particular mobile target, such as this answer for iOS: https://forum.unity.com/threads/how-to-capturing-device-logs-on-ios.529920/ or this answer for Android: https://forum.unity.com/threads/how-to-capturing-device-logs-on-android.528680/

    If you are working in VR, it might be useful to make your on onscreen log output, or integrate one from the asset store, so you can see what is happening as you operate your software.

    Another useful approach is to temporarily strip out everything besides what is necessary to prove your issue. This can simplify and isolate compounding effects of other items in your scene or prefab.

    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:

    https://forum.unity.com/threads/coroutine-missing-hint-and-error.1103197/#post-7100494

    "When in doubt, print it out!(tm)" - Kurt Dekker (and many others)

    Note: the
    print()
    function is an alias for Debug.Log() provided by the MonoBehaviour class.
     
  4. julien83franceschi

    julien83franceschi

    Joined:
    Nov 14, 2020
    Posts:
    178
    I don't understand your question.

    I'd like one of the GameObjects of the array to rotate (with transform.RotateAround) when a capsule is clicked.

    Thanks for your help,

    A+
     
    Last edited: Jun 26, 2023
  5. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    3,043
    I believe that part was obvious. If you can't interpret the question(s), I'm not sure if I can help.
     
  6. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    20,082
    Add a Debug.Log() to the method and see if anything appears in the console when you click the object.
     
    orionsyndrome likes this.
  7. zulo3d

    zulo3d

    Joined:
    Feb 18, 2023
    Posts:
    510
    Code (CSharp):
    1.     private Main mainScript;
    2.     // Start is called before the first frame update
    3.     void Start()
    4.     {
    5.         mainScript = (Main)FindObjectOfType(typeof(Main));
    6.     }
    7.  
    8.     void OnMouseDown()
    9.     {
    10.         mainScript.prefabs[0].transform.RotateAround(Vector3.zero, Vector3.up, 35);
    11.     }
     
  8. julien83franceschi

    julien83franceschi

    Joined:
    Nov 14, 2020
    Posts:
    178
    Hello everyone,

    I've entered your code correctly (see below):

    I put rotateRight instead of mainScript (the file where the code is)

    Code (CSharp):
    1.    private rotateRight mainScript;
    2.  
    3.     void Start()
    4.     {
    5.         mainScript = (rotateRight)FindObjectOfType(typeof(rotateRight));
    6.     }
    7.  
    8.     void OnMouseUp()
    9.     {
    10.          mainScript.prefabs[0].transform.RotateAround(Vector3.zero, Vector3.up, 35);
    11.     }
    The problem is that prefabs[0] doesn't rotate.

    If you have some code,

    it's most welcome.

    A+
     
  9. zulo3d

    zulo3d

    Joined:
    Feb 18, 2023
    Posts:
    510
    You said the prefabs array was defined in a scipt named 'Main' and the mousedown rotation code was in a script named 'rotationRight'. The code I posted looks for the prefabs array on a script/class named Main.
     
  10. julien83franceschi

    julien83franceschi

    Joined:
    Nov 14, 2020
    Posts:
    178
    Sorry, I did put the following code in a Main.cs file:

    private rotateRight mainScript;
    Code (CSharp):
    1.     void Start()
    2.     {
    3.         mainScript = (rotateRight)FindObjectOfType(typeof(rotateRight));
    4.     }
    Then I put the rest of the code here (rotateRight.cs file):

    Code (CSharp):
    1. mainScript.prefabs[0].transform.RotateAround(Vector3.zero, Vector3.up, 35);
    If you've got better code?

    Thank you very much,

    A+
     
  11. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,769
    If these objects are in the same scene, there's no need to use FindObjectOfType (especially the non-generic version???).

    Just expose a field in the inspector, and assign a reference to the right component.
     
  12. julien83franceschi

    julien83franceschi

    Joined:
    Nov 14, 2020
    Posts:
    178
    Hello everyone,

    I don't understand your message,

    Objects are stored in a public array of GameObjects, then created in the scene in the code with Instantiate.

    if you have some code I might understand better.

    Thanks a lot,

    A+
     
    Last edited: Jun 29, 2023