PickingUp object

Hey Everyone,

I am trying to implement a game where the player can pickup some objects and place them somewhere else. I need the player to pick one object at a time. I wrote some code to implement this but it is not working properly can anyone help me.

problem : The code works perfectly for only one object and gives trouble for other objects.

The below code is for the player object :

 public LayerMask layers;
 public  ObjectControler [] scripts;

 public float rayDistance;
 public KeyCode pickWith = KeyCode.P;

 bool ray;
bool held;

 private void Update()
 {
    ray = Physics.Raycast(transform.position, transform.forward, rayDistance, layers);
    for (int i = 0; i < scripts.Length; i++)
    {
        if (ray && !held && Input.GetKeyDown(pickWith) && !scripts[i].isTriggered)
        {
            scripts[i].isTriggered = true;
            held= true;
        }
        else if (held && scripts[i].isTriggered && Input.GetKeyDown(pickWith))
        {
            scripts[i].isTriggered = false;
            held = false;
        }
    }
 }

The below code is for the objects that the player can pick :

 public GameObject objectHolder;
 public Transform objectTransform;
public bool isTriggered;

 Rigidbody rb;

 private void Start()
 {
     rb = GetComponent<Rigidbody>();
 }

 private void Update()
 {

     if(isTriggered && transform.parent!=objectHolder)
     {
         AttachToParent();
     }

     if (isTriggered)
     {
         rb.position=objectTransform.position;
     }
     else if(!isTriggered)
     {
         transform.SetParent(null);
     }

 }

 void AttachToParent()
 {
     transform.SetParent(objectHolder.transform);
 }

I think the problem is on the player’s code where i used a for loop to traverse among the scripts i had. Can anyone please solve this issue.

That’s the best repro case ever! Now find out what is different.

Sounds like you wrote a bug… and that means… time to start debugging!

By debugging you can find out exactly what your program is doing so you can fix it.

Use the above techniques to get the information you need in order to reason about what the problem is.

You can also use Debug.Log(...); statements to find out if any of your code is even running. Don’t assume it is.

Once you understand what the problem is, you may begin to reason about a solution to the problem.

Remember with Unity the code is only a tiny fraction of the problem space. Everything asset- and scene- wise must also be set up correctly to match the associated code and its assumptions.

You don’t need to place a script on the objects. When picking up an object you can make it kinematic and then it will move along with the player.

using UnityEngine;
public class PickupObjects : MonoBehaviour
{
    Rigidbody held;
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Q))
        {
            if (held)
            {
                held.transform.parent = null;
                held.interpolation = RigidbodyInterpolation.Interpolate;
                held.isKinematic = false;
                held = null;
            }
            else if (Physics.Raycast(transform.position, transform.forward, out RaycastHit hit, 20))
            {
                held = hit.transform.GetComponent<Rigidbody>();
                held.interpolation = RigidbodyInterpolation.None;   // do this or movement may be out of sync
                held.isKinematic = true;
                held.transform.parent = transform;
            }
        }
    }
}

Thanks, It worked as i want with couple of changes.