Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by andreighinea1 · Mar 10, 2016 at 08:44 PM · cameraobjectreferenceinstancecamera follow

CameraController (object reference not set to an instance of an object)

I followed the roll-a-ball tutorial and camera worked and followed the player, but when I made the game multiplayer and it was spawning players as Player(Clone), I tried to remake the script as following (because it's not working if I put a prefab onto the main camera):

using UnityEngine; using System.Collections;

public class CameraController : MonoBehaviour {

 private GameObject rb;

 private Vector3 offset;

 void Start ()
 {
     GameObject player = GameObject.FindWithTag ("Player");
     player = rb;
     rb.GetComponent<GameObject> ();
     offset = transform.position - rb.transform.position;
 }

 void LateUpdate ()
 {
     transform.position = rb.transform.position + offset;
 }

}

And it was giving me this error (object reference not set to an instance of an object) ... I don't know what I did wrong... I'm appreciating any response ....

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
2

Answer by Zoogyburger · Mar 10, 2016 at 10:51 PM

Why are you making the RigidBody equal to the player? Try something more like this:

  private Transform Player;
  private Vector3 offset;
  void Start ()
  {
      Player = GameObject.FindWithTag ("Player").transform;
      Player = rb;
      rb.GetComponent<GameObject> ();
      offset = transform.position - rb.transform.position;
  }
  void LateUpdate ()
  {
      transform.position = rb.transform.position + offset;
  }

Also here is a real simple camera follow player script:

     public Transform target;
 
     void Update()
     {
         if (target)
         {
             transform.position = Vector3.Lerp (transform.position, target.position, 0.1f) + new Vector3 (0, 0, -10);
         }
     }
 }


Comment
Add comment · Show 12 · 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 andreighinea1 · Mar 11, 2016 at 12:49 PM 0
Share

It's not working... If I use the first method it's giving me the same error and if I use the second method it's not seeing the player and it's not following it ... That's maybe because I need to put a prefab to the camera and that's why it's not working like this ..... But thanks anyway !

avatar image Zoogyburger · Mar 11, 2016 at 05:25 PM 1
Share

@andreighinea1 Is it a 2d game or a 3d game? Because this scrip was sorta setup for a 2d game. Try this ins$$anonymous$$d:

 public Transform target;
 
     void Update()
     {
         if (target)
         {
             transform.position = Vector3.Lerp (transform.position, target.position, 0.1f);
         }
     }
 }

$$anonymous$$ake sure the script is on the $$anonymous$$ain Camera and in its Inspector set the Target to be the Player.

avatar image andreighinea1 Zoogyburger · Mar 13, 2016 at 09:28 AM 0
Share

It's a 3D game and I'm using the Network$$anonymous$$anager and when I put the player prefab into the Network$$anonymous$$anager it's spawning it with the camera but it's not following the player And i also searched for a 3d script for cameracontroller and I found that: using UnityEngine; using System.Collections;

public class CameraController : $$anonymous$$onoBehaviour { public GameObject target; public float smoothing = 2f; public Vector3 offset;

 void Start()
 {
 }

 void FixedUpdate()
 {
     if(GameObject.FindGameObjectWithTag("Player") == target)
     {
         offset = new Vector3(0.0f, target.GetComponent<PlayerController>().size / 2 + 15, -target.GetComponent<PlayerController>().size / 2- 20);
     }
         
         transform.position = target.transform.position + offset;
         //transform.LookAt(target.transform.position);
 }

}

But it says when I make it 'networked' it's not following the player when I assign to it a prefab of the player ... But if I put the player into the scene and I assign it from scene it's working ... But I want to make it multiplayer ....

avatar image Zoogyburger · Mar 14, 2016 at 05:20 PM 1
Share

Ok, don't assign the Target at all. Ins$$anonymous$$d tag your player "Player" and update your Camera script to this:

     public Transform target;
 
     void Update()
     {
         target = GameObject.FindWithTag ("Player").transform;
         if (target)
         {
             transform.position = Vector3.Lerp (transform.position, target.position, 0.1f)+ new Vector3 (0, 0, -10);
         }
     }
 }
avatar image andreighinea1 Zoogyburger · Mar 15, 2016 at 07:38 PM 0
Share

Thank you very much ! This script really helped me ! (I had to replace the Update with FixedUpdate) Now it's following the player, yeah it's still giving me the error object reference not .... But for me is good at least that its working, one single thing is that if I go for something like 1000 meters down with speed the player is not visible at all ... But for now is good, because the player can stop for a little to wait for camera ...

avatar image Zoogyburger · Mar 15, 2016 at 08:07 PM 1
Share

Hey @andreighinea1 Glad my script helped you. It's really weird that you still get that error and the Camera lags. Here is my complete script (Try just Update again)

 public Transform target;
 
     void Update()
     {
         target = GameObject.FindWithTag ("Player").transform;
         if (target)
         {
             transform.position = Vector3.Lerp (transform.position, target.position, 1f)+ new Vector3 (0, 0, -10);
         }
     }
 }
avatar image Zoogyburger · Mar 16, 2016 at 07:38 PM 1
Share

Replace the trouble line with this:

 Player = GameObject.Instantiate(Player) as Transform;

And assign the Camera target to be your Player Prefab.

Show more comments

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

64 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Object follows AR Camera 1 Answer

How to Fix this issue 1 Answer

HTC Vive camera deformed when assigned a parent 0 Answers

Objects close to camera jitter? 0 Answers

My camera jitters a lot. Any fix? 0 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