2

I have managed to snap my game object to the bottom of another game object, but once I have instantiated a second game object, it doesn't snap to the instantiated game object. I want to be able to have the instantiated game object to snap at the bottom of another instantiated game object once it has been loaded, similar to a daisy chain. Currently, it will only snap to the original "bottom" game object but not the second. Any suggestion?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SnapToMe : MonoBehaviour
{
    public GameObject target = null;
    public float snapDistance = 2f;

    void FixedUpdate()
    {
        if (target == null) return;

        if (Vector3.Distance(transform.position, target.transform.position) <= snapDistance)
        {
            target.transform.parent = transform;
            target.transform.localRotation = Quaternion.identity;
            target.transform.localPosition = Vector3.zero;
            target.GetComponent<DragObject>().enabled = false;
        }
    }
}

public void LoadDrillString()
{
    if (!isCreatedDrillbit)
    {
        GameObject instance = Instantiate(Resources.Load("Drillstring-2", typeof(GameObject))) as GameObject; 
        GameObject TipSnapPoint = GameObject.Find("Bottom");
        SnapToMe tipsnapper = TipSnapPoint.GetComponent<SnapToMe>();
        tipsnapper.target = instance;
        tipsnapper.snapDistance = 2f;
    }
}

enter image description here

Coder
  • 499
  • 3
  • 13
  • 29
  • `GameObject.Find()` searches the entire hierarchy and returns the first `gameObject` whose name matches the parameter. Based on your sample, this will always return the same object. You need a better way to find the correct object to assign to `target`. – 3Dave Feb 11 '20 at 18:57
  • I see. I have created various Game.Object.Find() before, but wanted a create a universal one that doesn't require a specific game.object to snap on. – Coder Feb 11 '20 at 19:08
  • 1
    Consider creating a type of custom `Connector` class, then searching for that using e.g. `FindObjectsOfType`, and looping through all results, taking the first one where a custom public property `connector.slot` is still null -- then when assigning it to that one, fill the slot with a reference to your object. Last time I created a connector class, I also added a custom Enum to define the connection type (square, circle, plus etc.) which different kinds of objects could use to determine what other objects they can connect to. – Philipp Lenssen Feb 11 '20 at 19:43
  • 1
    Thanks for the info Philipp, I'm gonna set something up and see if that will work. – Coder Feb 11 '20 at 19:44
  • Great. I provided a somewhat longer explanation of my connection system in [this answer](https://stackoverflow.com/questions/57400226/how-to-stick-an-object-to-another-objects-position-while-working-on-unity-for-ht/57402290#57402290). That would be for drag & drop in VR, though, so perhaps too much overhead, and you can use something much simpler. – Philipp Lenssen Feb 11 '20 at 19:46
  • Is there an easier approach that would allow a game object to snap to any top-bottom connector without getting overly complicated? I just wanted any game object to attach to the bottom and top of another game object. – Coder Feb 11 '20 at 19:48
  • Ok, I will look into your VR example. Again, thanks! – Coder Feb 11 '20 at 19:50
  • Did you try using collider? @Coder – Ghost The Punisher Feb 12 '20 at 07:43
  • Yes, I have used collider. – Coder Feb 12 '20 at 12:58

0 Answers0