Hello everyone.

The Mixed Reality Forums here are no longer being used or maintained.

There are a few other places we would like to direct you to for support, both from Microsoft and from the community.

The first way we want to connect with you is our mixed reality developer program, which you can sign up for at https://aka.ms/IWantMR.

For technical questions, please use Stack Overflow, and tag your questions using either hololens or windows-mixed-reality.

If you want to join in discussions, please do so in the HoloDevelopers Slack, which you can join by going to https://aka.ms/holodevelopers, or in our Microsoft Tech Communities forums at https://techcommunity.microsoft.com/t5/mixed-reality/ct-p/MicrosoftMixedReality.

And always feel free to hit us up on Twitter @MxdRealityDev.
Options

How to use the same canvas to manipulate numerouse game scene object

This maybe an obvious question but I can't get out of it ...
As of today, I have a small UI that I want to use as my general UI to manipulate various game objects.
This is my situation:
I have a cube and the canvas is the cube's child. The canvas appears and disappears when I click on the cube.
On the canvas as buttons to manipulate scale and rotation.Each button has the Button manager script with public GameObject manipulateObj and in Unity i assign the cube to it. This way, the cubes know which object they have to manipulate.
Now, I want to change the situation and I want to have a canvas that is indipendent from any other game object. I was able to move the canvas from one object to another. What I can't do is how to make buttons understand that I want to manupilate the cube i just clicked.
I have to modify the OnInputClicked method of the Cube and from this, I want to pass this game object to my Buttonmanager script and I was wondering if someone can help me out.

Best Answer

  • Options
    keljedkeljed ✭✭
    Answer ✓

    I'm not sure about your question. Do you ask how to pass the cube game object to the button script or how you can avoid it?

    If you want to avoid passing objects to your script and when you are using the HoloToolkit, you could modify the InputManager to remember the last clicked game object. It is a singelton so you can access it from every other script.

    If you don't know how to pass the cube to your script, you could make your script a singleton and provide a method or property to set the clicked game object.

Answers

  • Options
    keljedkeljed ✭✭
    Answer ✓

    I'm not sure about your question. Do you ask how to pass the cube game object to the button script or how you can avoid it?

    If you want to avoid passing objects to your script and when you are using the HoloToolkit, you could modify the InputManager to remember the last clicked game object. It is a singelton so you can access it from every other script.

    If you don't know how to pass the cube to your script, you could make your script a singleton and provide a method or property to set the clicked game object.

  • Options

    @keljed
    hi! in the script attached to my cube I created a method getObject that returns this.gameObject.

    In my button manager on the method used when I click the button i do
    Debug.Log(om.getObject().name) where is defined ObjectManager om but I get a an exception that states this: Object reference not set to an instance of an object.
    I maybe doing something wrong but I just can't figure it out. it must be the way I handle ObjectManager in the buttonmanagerScript> @keljed said:

    I'm not sure about your question. Do you ask how to pass the cube game object to the button script or how you can avoid it?

    If you want to avoid passing objects to your script and when you are using the HoloToolkit, you could modify the InputManager to remember the last clicked game object. It is a singelton so you can access it from every other script.

    If you don't know how to pass the cube to your script, you could make your script a singleton and provide a method or property to set the clicked game object.

    @keljed said:
    I'm not sure about your question. Do you ask how to pass the cube game object to the button script or how you can avoid it?

    If you want to avoid passing objects to your script and when you are using the HoloToolkit, you could modify the InputManager to remember the last clicked game object. It is a singelton so you can access it from every other script.

    If you don't know how to pass the cube to your script, you could make your script a singleton and provide a method or property to set the clicked game object.

    @keljed said:
    I'm not sure about your question. Do you ask how to pass the cube game object to the button script or how you can avoid it?

    If you want to avoid passing objects to your script and when you are using the HoloToolkit, you could modify the InputManager to remember the last clicked game object. It is a singelton so you can access it from every other script.

    If you don't know how to pass the cube to your script, you could make your script a singleton and provide a method or property to set the clicked game object.

  • Options

    @keljed : i stil can't solve it.
    So I made my ButtonManager a singleton since it is the same throughout the whole application.

    It has a public variable define as:
    public GameObject manipulateObject;

    now how am i supposed to give manipulateObject the "cube" value.
    In the Objectmanager attached to the cube I have : Buttonmanager.Instance.manipulateObj = gameObject;

    but then when i try Debug.Log(manipulateObject.name) i get the exception and have this "Object reference not set to an instance of an object."
    am i suppose to instantiate the Buttonmanager class? I guess the error is there, I just can't get it.

  • Options
    keljedkeljed ✭✭
    edited March 2017

    I don't know why manipulateObj is null. Did you set breakpoints in the code to understand what's going on?
    You could post the code of ButtonManager and ObjectManager. Maybe an additional pair of eyes can find another source for the problem.

  • Options

    `public class Buttonmanager : MonoBehaviour{

    public GameObject manipulateObj;
    public GameObject parentCanvas;
    private Vector3 zero;
    
    private void Awake()
    {
        manipulateObj = new GameObject();
    }
    
    public void getObj(GameObject obj)
    {
        //manipulateObj = new GameObject();
        this.manipulateObj = obj;
        Debug.Log(manipulateObj.name);
    }
    
    public void biggerButton()
    {
        //manipulateObj.transform.localScale += new Vector3(0.01f, 0.01f, 0.01f);
        //parentCanvas.transform.localScale += new Vector3(0.01f, 0.01f, 0.01f);
        Debug.Log(manipulateObj.name);
    }
    
    public void smallerButton()
    {
        zero = new Vector3(0, 0, 0);
        if (manipulateObj.transform.localScale != zero)
        {
            manipulateObj.transform.localScale -= new Vector3(0.01f, 0.01f, 0.01f);
            //parentCanvas.transform.localScale -= new Vector3(0.01f, 0.01f, 0.01f);
        }
    }
    
    public void rotationLeft()
    {
        manipulateObj.transform.Rotate(new Vector3(0, 2f,0));
        Debug.Log(manipulateObj.transform.rotation);
        //Debug.Log("PAPPAPPEEEROOOOOO");
    }
    
    public void rotationRight()
    {
        manipulateObj.transform.Rotate(new Vector3(0,-2f, 0));
        //Debug.Log("PAPPAPPEEEROOOOOO");
    }
    
    public void rotation_Up()
    {
        manipulateObj.transform.Rotate(new Vector3(2f, 0, 0));
        //parentStabilized.transform.Rotate(new Vector3(0, 0, 0));
        //Debug.Log("PAPPAPPEEEROOOOOO");
    }
    
    public void rotation_Down()
    {
        manipulateObj.transform.Rotate(new Vector3(-2f, 0, 0));
        //Debug.Log("PAPPAPPEEEROOOOOO");
    }
    
    public void Close()
    {
        parentCanvas.SetActive(false);
    }
    

    }`

    OBJECT MANAGER
    `public class ObjectManager : MonoBehaviour, IInputClickHandler,IFocusable {

    public GameObject showCanvases;
    Buttonmanager bm = new Buttonmanager();
    
    private bool state = false;
    
    public void OnFocusEnter()
    {       
    }
    
    public void OnFocusExit()
    { 
    }
    
    public void OnInputClicked(InputClickedEventData eventData)
    {
        //state = !state;  
        showCanvases.SetActive(true);
        showCanvases.transform.position = gameObject.transform.position;
        bm.getObj(gameObject);
    }
    

    }`

    The button manager is attached to each buttons and every method except getObj is used by the buttons.
    the getObj works fine, I just don't understand why manipulateObj value is the same as it's value when it was created in the awake even if getObj works fine. It seems like manipulateObj = obj in the getObj method works only inside that method and out of it, manipulateObj is another object.
    Is this because I'm using a button from unity's UITools???

  • Options

    the problem is that I actually have two different object since I create new Button manager in the Objectmanager script but how can I solve this problem?

Sign In or Register to comment.