- Home /
I have problem with getcomponent
Hi! I want to get target form CameraControl.cs to Player.cs here is my script
CameraControl.cs
using UnityEngine;
using System.Collections;
public class CameraControl : MonoBehaviour {
RaycastHit hit;
bool leftClickFlag = true;
public GameObject actor;
public string floorTag;
public Vector3 target;
// Update is called once per frame
void Update () {
if (Input.GetKey(KeyCode.Mouse0) && leftClickFlag)
leftClickFlag = false;
if (!Input.GetKey(KeyCode.Mouse0) && !leftClickFlag)
{
leftClickFlag = true;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit, 1500))
{
if (hit.transform.tag == floorTag)
{
float X = hit.point.x;
float Z = hit.point.z;
target = new Vector3(X, actor.transform.position.y, Z);
}
}
}
}
}
Player.cs
using UnityEngine;
using System.Collections;
public class Player : Character {
public Vector3 Pos;
private CameraControl CameraControlSc;
/* ============================== CONTROLS ============================== */
void Awake ()
{
CameraControlSc = GetComponent<CameraControl>();
}
public void Update () {
isLeft = false;
isRight = false;
isUp = false;
isDown = false;
print ("Target is"+CameraControlSc.target);
UpdateMovement();
}
/* ============================== TRIGGER EVENTS ====================================================================== */
void OnTriggerEnter(Collider other)
{
// did the player collide with a pickup?
// pickups and scoring will be added in an upcomming tutorial
if (other.gameObject.CompareTag("Pickup"))
{
if (other.GetComponent<Pickup>())
{
other.GetComponent<Pickup>().PickMeUp();
xa.sc.Pickup(); // tell Scoring.cs that we collected a pickup
}
}
}
}
After run console say
NullReferenceException: Object reference not set to an instance of an object Player.Update () (at Assets/Scripts/Player.cs:23)
Please tell me what Did I Do Wrong, thank you
Answer by KellyThomas · Jan 12, 2014 at 09:53 AM
That error is telling you that one line 23 CameraControlSc holds a null value.
The only way that this can occur is if on line 12 GetComponent() returns null meaning it was unable to find CameraControl.
When you call GetComponent() without any further quantification it is equivalent to calling this.gameObject.GetComponent() and performs a search on the game object that the script it attached to.
If your CameraControl is on another game object then you should find this other object first:
//assuming the camera in on a game object named "MainCamera"
GameObject cameraObject = GameObject.Find("MainCamera");
CameraControlSc = cameraObject.GetComponent<CameraControl>();
Great! It's work Thank you very much,$$anonymous$$elly$$anonymous$$ :)
Your answer
Follow this Question
Related Questions
Collection Using "GetComponent" 1 Answer
Transfering variable between gameojects and scripts 2 Answers
How to get script component of an object with out using its name? 2 Answers
Help with accessing scoring system variable C# plz. 3 Answers
FindGameObjectsWithTag Changing Variable on Another Script Not Fully Working 2 Answers