0
\$\begingroup\$

I'm trying to pass a gameobject from one script to another runtime attached script. My fisrt script is attached to the FirstPlayerController and the gameobject is not dragged in the editor, but is picked up and carried in the script. All i want is to pass the gameobject to the CameraController class to as make the second camera follow that object. Below is what i've tried so far. I dont want to access the gameobject with the tag, because there are more gameobjects that can be picked up. I want to access the carried object to the cameracontroller class.

Thanks!

Pickupobject.cs

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

public class pickupobject : MonoBehaviour
{
    GameObject mainCamera;

    bool carrying;

    public GameObject carriedObject;

    public float distances;

    public float smooth;

    float speed = 1000f;

    new Camera camera;





    // Use this for initialization
    void Start()
    {

        mainCamera = GameObject.FindWithTag("MainCamera");

        camera = GameObject.FindWithTag("secondCamera").GetComponent<Camera>();

        camera.enabled = false;



    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.T) && carrying)
        {
            carrying = !carrying;

            ThrowBall();
        }
        if (carrying)
        {
            carry(carriedObject);

        }
        else
        {
            pickup();
        }


    }

    private void pickup()
    {
        if (Input.GetKeyDown(KeyCode.E))
        {

            int x = Screen.width / 2;

            int y = Screen.height / 2;

            Ray ray = mainCamera.GetComponent<Camera>().ScreenPointToRay(new Vector3(x, y));

            RaycastHit hit;

            if (Physics.Raycast(ray, out hit))
            {
                pickupable p = hit.collider.GetComponent<pickupable>();

                if (p != null)
                {
                    carrying = true;

                    carriedObject = p.gameObject;

                    //camera.enabled = true;

                   // camera.gameObject.AddComponent<CameraController>();

                    //carriedObject.AddComponent<MovingBall>();


                }
            }
        }
    }

    void carry(GameObject o)
    {
        o.GetComponent<Rigidbody>().isKinematic = true;

        o.transform.position = mainCamera.transform.position + mainCamera.transform.forward * distances;

    }


    //void CheckDrop()
    //{
    //    if (Input.GetKeyDown(KeyCode.U))
    //    {
    //        Drop();
    //    }
    //}

    //void Drop()
    //{
    //    ThrowBall();

    //}
    void ThrowBall()
    {


        mainCamera.SetActive(false);

        camera.enabled = true;

       // carriedObject.AddComponent<CameraController>();

        camera.gameObject.AddComponent<CameraController>();

        carriedObject.AddComponent<MovingBall>();

        carriedObject.GetComponent<Rigidbody>().isKinematic = false;

        carriedObject.GetComponent<Rigidbody>().AddForce(0f, 0f, speed);

     }


}

CameraController.cs

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

public class CameraController : MonoBehaviour {

   public GameObject icosphere;

    public GameObject abc;
    Vector3 offset;


    // Use this for initialization


    void Start () {

        icosphere = GameObject.FindWithTag("purpleball");

        offset = transform.position - icosphere.transform.position;
        }

    // Update is called once per frame
    void Update ()
    {

            transform.position = icosphere.transform.position + offset;
    }
}
\$\endgroup\$
1
\$\begingroup\$

When you AddComponent<CameraController>() store that result in a variable like CameraController temp and then call a method on the camera controller to pass in the object. That means you need to add a new public method to CameraController to call, something like:

public void SetCarriedObject(GameObject obj) {
  // do whatever
}

Now in the first script you can write temp.SetCarriedObject(carriedObject);

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ you mean to say, i have to make a CameraContoller class instance after attaching the second camera to the CameraController class? and call the method as you mentioned in your answer? \$\endgroup\$ – Salar Muhammad May 1 '17 at 15:57
  • \$\begingroup\$ No I'm not saying to make another instance, I'm saying to keep a reference to the instance you created. AddComponent returns the component (ie. class instance) just created, but your code isn't bothering to catch that return. \$\endgroup\$ – jhocking May 1 '17 at 17:08
  • \$\begingroup\$ so the line after adding that: CameraController temp = camera.gameObject.AddComponent<CameraController>(); \$\endgroup\$ – jhocking May 1 '17 at 17:09
  • \$\begingroup\$ Wonderful! @jhocking you made it look so easy. I've been after that for 2 days. Thanks a ton Friend :) \$\endgroup\$ – Salar Muhammad May 1 '17 at 17:17
  • \$\begingroup\$ It is somewhat obscured in Unity's learning tutorials and such that components are simply code objects that operate like normal in OOP. \$\endgroup\$ – jhocking May 1 '17 at 17:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.