Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by IamMai · Jan 12, 2014 at 09:41 AM · getcomponent

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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>();
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image IamMai · Jan 12, 2014 at 10:22 AM 0
Share

Great! It's work Thank you very much,$$anonymous$$elly$$anonymous$$ :)

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

19 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges