4

Following this tutorial, I can position an object in space.

How can I replace the object with another one in the same position? I need to have a public function and assign it to a button, so when I press the button, the "Kitty" model being replaced with another model.

Here is the main script in the tutorial:

using UnityEngine;
using System.Collections;

public class KittyUIController : MonoBehaviour
{
    public GameObject m_kitten;
    private TangoPointCloud m_pointCloud;

    void Start()
    {
        m_pointCloud = FindObjectOfType<TangoPointCloud>();
    }

    void Update ()
    {
        if (Input.touchCount == 1)
        {
            // Trigger place kitten function when single touch ended.
            Touch t = Input.GetTouch(0);
            if (t.phase == TouchPhase.Ended)
            {
                PlaceKitten(t.position);
            }
        }
    }

    void PlaceKitten(Vector2 touchPosition)
    {
        // Find the plane.
        Camera cam = Camera.main;
        Vector3 planeCenter;
        Plane plane;
        if (!m_pointCloud.FindPlane(cam, touchPosition, out planeCenter, out plane))
        {
            Debug.Log("cannot find plane.");
            return;
        }

        // Place kitten on the surface, and make it always face the camera.
        if (Vector3.Angle(plane.normal, Vector3.up) < 30.0f)
        {
            Vector3 up = plane.normal;
            Vector3 right = Vector3.Cross(plane.normal, cam.transform.forward).normalized;
            Vector3 forward = Vector3.Cross(right, plane.normal).normalized;
            Instantiate(m_kitten, planeCenter, Quaternion.LookRotation(forward, up));
        }
        else
        {
            Debug.Log("surface is too steep for kitten to stand on.");
        }
    }
}
Rumata
  • 1,027
  • 3
  • 16
  • 47

2 Answers2

2

Added one method ReplaceKitten<= Call this on your button click.
Added a public yourOtherModel GameObjectfor the desired model.
Made the Instantiated kitten global (into a variable) from the PlaceKitten method.

    using UnityEngine;
    using System.Collections;

    public class KittyUIController : MonoBehaviour
    {
        public GameObject m_kitten;
        public GameObject yourOtherModel; // could be prefab
        private TangoPointCloud m_pointCloud;
        private GameObject createdKitten;

        void Start()
        {
            m_pointCloud = FindObjectOfType<TangoPointCloud>();
        }

        void Update ()
        {
            if (Input.touchCount == 1)
            {
                // Trigger place kitten function when single touch ended.
                Touch t = Input.GetTouch(0);
                if (t.phase == TouchPhase.Ended)
                {
                    PlaceKitten(t.position);
                }
            }
        }
        //This method will disable the kitten and instantiate or place a GameObject on the same spot.
        public void ReplaceKitten()
        {
            GameObject modelToReplace;
            //Do this only if you will pass a Prefab to this Script
            modelToReplace = Instantiate(yourOtherModel);
            //Set your new object at the same coordinates and reset position
            modelToReplace.transform.parent = m_kitten.transform;
            modelToReplace.transform.position = new Vector3(0,0,0);
            //Disable kitten
            m_kitten.SetActive(false);
        }

        void PlaceKitten(Vector2 touchPosition)
        {
            // Find the plane.
            Camera cam = Camera.main;
            Vector3 planeCenter;
            Plane plane;
            if (!m_pointCloud.FindPlane(cam, touchPosition, out planeCenter, out plane))
            {
                Debug.Log("cannot find plane.");
                return;
            }

            // Place kitten on the surface, and make it always face the camera.
            if (Vector3.Angle(plane.normal, Vector3.up) < 30.0f)
            {
                Vector3 up = plane.normal;
                Vector3 right = Vector3.Cross(plane.normal, cam.transform.forward).normalized;
                Vector3 forward = Vector3.Cross(right, plane.normal).normalized;
                createdKitten = Instantiate(m_kitten, planeCenter, Quaternion.LookRotation(forward, up));
            }
            else
            {
                Debug.Log("surface is too steep for kitten to stand on.");
            }
        }
    }

Make sure there are no typos I'm not on an editor.

Hristo
  • 1,805
  • 12
  • 21
  • 1
    Note that when you make the texts yellow, it means that you are quoting what OP said and replying to that. In the case of your answer, you were not quoting OP so I removed the quotes. – Programmer Jul 03 '17 at 07:39
  • Thank you very much! But there is an error - looks like it can't accept "planeCenter" in ReplaceKitten() - http://imgur.com/a/n4l9J – Rumata Jul 06 '17 at 17:54
  • @Rumata Oh yeah I now see that there is no declaration of `planeCenter`. That variable was just meant for declaring the instantiation `position` of the object which we change afterwords. In other words try using `Vector3.zero` and it should do the job! – Hristo Jul 06 '17 at 20:26
  • @Hristo Can You explain in more detail, please? I tried: modelToReplace = Instantiate(yourOtherModel, Vector3.zero); and now I have multiple errors: http://imgur.com/a/No3ub – Rumata Jul 06 '17 at 20:42
  • @Rumata Updated the code, removed the `Transform` parent from the constructor of `instantiate`. Now using the first constructor with no _parent_. – Hristo Jul 06 '17 at 20:56
  • @Hristo Thank you! 2 errors gone, 2 left: http://imgur.com/a/6CbWL It is related to this lines: http://imgur.com/a/zjHyn – Rumata Jul 06 '17 at 21:04
  • @Rumata Excuses I had no editor, try the code now :) – Hristo Jul 06 '17 at 21:29
0

Save the reference of the object that you have instantiated and whenever you want to add a new object check that whether the instance is null or not,

if not null than that means object is in the space. destroy it and create a new instance of the gameobject that you want and save its reference.

if null than that means space is empty create a new instance and save its reference

 public GameObject OtherModel;   // new model that you want to replace with kitty
 GameObject myRef;               // Reference for the object that is already in scene

    void PlaceKitten(Vector2 touchPosition)
    {
        // Find the plane.
        Camera cam = Camera.main;
        Vector3 planeCenter;
        Plane plane;
        if (!m_pointCloud.FindPlane(cam, touchPosition, out planeCenter, out plane))
        {
            Debug.Log("cannot find plane.");
            return;
        }

        // Place kitten on the surface, and make it always face the camera.
        if (Vector3.Angle(plane.normal, Vector3.up) < 30.0f)
        {
            Vector3 up = plane.normal;
            Vector3 right = Vector3.Cross(plane.normal, cam.transform.forward).normalized;
            Vector3 forward = Vector3.Cross(right, plane.normal).normalized;

            // Check whether we have already placed something or not
            if (myRef != null)
            {
                Transform temp = myRef.transform;
                Destroy(myRef);
                myRef = Instantiate(OtherModel, temp.position, temp.rotation);
            }
            else
            {
                myRef = Instantiate(m_kitten, planeCenter, Quaternion.LookRotation(forward, up));
            }
        }
        else
        {
            Debug.Log("surface is too steep for kitten to stand on.");
        }
    }
Nipun David
  • 273
  • 2
  • 12
  • Thank you, but as I understand in this case it will work if I will tap the screen again. And in this case, it most likely will be a slightly different location where there is no "Kitty" yet, and I will keep cloning "Kitty" model. While I need to place the "Kitty" only once, and then replace it with another models (in the same location) by clicking a button. – Rumata Jun 30 '17 at 12:14
  • No, it will clone kitty if already in the space, as it will first check that whether there is a kitty or not and if not then it will clone and if there it is a kitty it will clone a new gameobject on the same location with the help of myRef variable. See code :) – Nipun David Jun 30 '17 at 15:47