0

i am building a simulation that requires a search box in which the user can type in the name of a game object and the camera should automatically zoom in on this object in the Unity 5 scene. However i have tried multiple scripts with no luck.

The below code is what i thought would work to solve this, however it did not!

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

public class drone_camera_lookup : MonoBehaviour {

    public float minX = -360.0f;
    public float maxX = 360.0f;

    public float minY = -45.0f;
    public float maxY = 45.0f;

    public float sensX = 100.0f;
    public float sensY = 100.0f;

    float rotationY = 0.0f;
    float rotationX = 0.0f;
    public GameObject drone;
    public GameObject actual;
    public GameObject instance;
    var gameObjectTag;

    void Update () {

          //  drone = Resources.Load("drone_with_controller") as GameObject;
            drone = GameObject.FindGameObjectWithTag(gameObjectTag);
            actual = this.drone.GetComponent<GameObject>();
            instance = Instantiate(actual, transform.position, transform.rotation) as GameObject;

            rotationX += instance.transform.localEulerAngles.x * sensX * Time.deltaTime;
            rotationY += instance.transform.localEulerAngles.y * sensY * Time.deltaTime;
            rotationY = Mathf.Clamp (rotationY, minY, maxY);
            transform.localEulerAngles = new Vector3 (-rotationY, rotationX, 0);    

    }
}

The error been show is : NullReferenceException: Object reference not set to an instance of an object

I would ideally like to make the script animate the camera to the object.Thank you in advance.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • `actual = this.drone.GetComponent();` makes no sense. `drone` is *already* a GameObject! In addition, GameObject does not extend Component and therefor this method returns null. – Draco18s no longer trusts SE Jan 09 '18 at 05:39
  • 1
    Possible duplicate of [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Draco18s no longer trusts SE Jan 09 '18 at 05:40
  • 1
    In addition to the comment of @Draco18s `GameObject.FindGameObjectWithTag(gameObjectTag);` most likely returns a null object if you did not initialize `gameObjectTag` anywhere else. – Serkan Pekçetin Jan 09 '18 at 10:04

1 Answers1

0

Hey guys i managed to solve my problem by adding this code to my camera, the script receives input from the input box and then uses the wrld3d API to animate the camera to the object, works quite smooth for me :-)

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using Wrld;
using Wrld.Space;

public class focus_drone : MonoBehaviour {
    public Text drone_input; 
    public Transform target;
    private Camera cameraX;

    void Start()
    {
   string drone_id = drone_input.text.ToString();
   target = GameObject.Find(drone_id).transform;
   transform.LookAt(target); 

    }

    void Update()
    {
        string drone_id = drone_input.text.ToString();
        // Rotate the camera every frame so it keeps looking at the target
        target = GameObject.Find(drone_id).transform;
        transform.LookAt(target);
        var destLocation = LatLong.FromDegrees(13.746863, 100.538847);
        Api.Instance.CameraApi.AnimateTo(destLocation, distanceFromInterest: 30, headingDegrees: 0, transitionDuration: 0.5, jumpIfFarAway: false);
    }
}

I hope whoever faces the same problem i did finds this useful.