I know my code is messy, I just started learning. I am trying to make it so an object locks onto the object it collides with. I used this script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CollisionTest : MonoBehaviour
{
public Vector2 Card1LockedPosition;
public Vector2 Card2LockedPosition;
public Vector2 Card3LockedPosition;
public Vector2 Card4LockedPosition;
public Vector2 Card5LockedPosition;
void Start()
{
if (gameObject.name == "Card1O")
{
Card1LockedPosition = new Vector2(20.5f, -7.1f);
}
if (gameObject.name == "Card2O")
{
Card2LockedPosition = new Vector2(20.5f, -7.1f);
}
if (gameObject.name == "Card3O")
{
Card3LockedPosition = new Vector2(20.5f, -7.1f);
}
if (gameObject.name == "Card4O")
{
Card4LockedPosition = new Vector2(20.5f, -7.1f);
}
if (gameObject.name == "Card5O")
{
Card5LockedPosition = new Vector2(20.5f, -7.1f);
}
}
void OnCollisionEnter2D(Collider col)
{
if (gameObject.name == "Card1O")
{
if (col.gameObject.tag == "Lockable")
{
Card1LockedPosition = col.gameObject.transform.position;
}
}
if (gameObject.name == "Card1O")
{
if (col.gameObject.tag == "Lockable")
{
Card2LockedPosition = col.gameObject.transform.position;
}
}
if (gameObject.name == "Card1O")
{
if (col.gameObject.tag == "Lockable")
{
Card3LockedPosition = col.gameObject.transform.position;
}
}
if (gameObject.name == "Card1O")
{
if (col.gameObject.tag == "Lockable")
{
Card4LockedPosition = col.gameObject.transform.position;
}
}
if (gameObject.name == "Card1O")
{
if (col.gameObject.tag == "Lockable")
{
Card5LockedPosition = col.gameObject.transform.position;
}
}
}
}
But when my object hits the collider, It doesn’t stick to it. The only thing is I see is this message: “Script error: OnCollisionEnter2D”. Can someone help me? It’s probably something stupid and easy but thanks anyways. (The script is on the object that should be locked on another object on collision, all the objects have 2D box colliders and they are all sprites).