Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

3d Set chosen activeGameObject in memory

Discussion in 'Scripting' started by phatpixels, Feb 20, 2018.

  1. phatpixels

    phatpixels

    Joined:
    Sep 18, 2013
    Posts:
    9
    Hello,
    i have looked up and down for an answer, before coming here. Im making a simple tank game.
    In game i have multiple 3d objects, i would like to raycast select a single object and move it into position, then immediately move to a UI menu button to rotate that same object 90 or 180 degrees. My issue is keeping that chosen object selected after dropping it in position and yet alter its rotation through UI menu button present in corner of screen.

    mySelection = Selection.activeGameObject;
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,517
    Are you using the UnityEditor.Selection class above? That's for the editor only, not for actual runtime use. You need to implement your own notion of which object currently is selected for in the game.

    As for rotation, it's always going to be easier to keep a single float indicating the compass heading rotation of the object (say about the Y axis) and then drive the object's rotation value from that float:

    Code (csharp):
    1.  myObject.transform.rotation = Quaternion.Euler( 0, angle, 0);
    Then your buttons would simply increment/decrement the "angle" variable when held down.
     
  3. phatpixels

    phatpixels

    Joined:
    Sep 18, 2013
    Posts:
    9

    Thankyou for your reply Kurt. The rotation part i understand, its retaining in memory the selected and moved object, so this item is rotated when i move cursor to the UI menu to rotate, change color etc. all in game mode.
     
  4. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    You need to store the reference somewhere. You can just make a static class if your use-case is simple and not looking to expand on the logic too much.

    Code (csharp):
    1.  
    2. public static class SelectionHelper
    3. {
    4.     public static GameObject currentSelectedObject = null;
    5. }
    6.  
    In your selection part, set the value:
    Code (csharp):
    1.  
    2. SelectionHelper.currentSelectedObject = your game object;
    3.  
    And in the UI part:
    Code (csharp):
    1.  
    2. void OnButtonClick()
    3. {
    4.     Transform selectedTransform = SelectionHelper.currentSelectedObject;
    5.  
    6.     if(selectedTransform != null)
    7.     {
    8.          selectedTransform.Rotate( whatever );
    9.     }
    10. }
    11.  
    Not a very elegant solution but simple enough for a beginner.
     
    phatpixels likes this.
  5. phatpixels

    phatpixels

    Joined:
    Sep 18, 2013
    Posts:
    9

    Thankyou groZZleR for firstly making sense of my question and responding with an answer that with just a little more effort on my behalf worked perfectly in the end.