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 UncleGubbsy · Apr 01, 2016 at 09:20 PM · cameravector3nullreferenceexceptionselectionselected

So I know this has been asked before but, I feel this only applies to my problem.

I get the "NullReferenceException: Object reference not set to an instance of an object" error. I usually know what to do but, at this point I cannot figure it out. I'm making a rts game and this code worked before but now gives me trouble. This is the Unit Script:

using UnityEngine;

using System.Collections;

public class Unit : MonoBehaviour {

 public bool selected = false;
 public CameraControl camControl;
 public float speed = 10;
 private Vector3 moveToDest = Vector3.zero;
 public float floorOffset = 1;
 public float stopDistanceOffset;
 public Vector3 destination;
 void Update () {
     //UpdateMove();
     if (GetComponent<Renderer>().isVisible && Input.GetMouseButtonUp(0))
     {
         Vector3 camPos = Camera.main.ScreenToViewportPoint(transform.position);
         camPos.y = camControl.InvertMouseY(camPos.y);
         selected = camControl.selection.Contains(camPos);
         if (selected)
         {
             GetComponent<Renderer>().material.color = Color.red;

         }
         else
         {
             GetComponent<Renderer>().material.color = Color.white;
         }
     }
     if(selected && Input.GetMouseButtonUp(1))
     {
         destination = camControl.GetDestination();
         if (destination != null)
         {
             Debug.Log("Can Move");
             moveToDest = destination;
             NavMeshAgent agent = GetComponent<NavMeshAgent>();
             agent.SetDestination(destination);
             Vector3 direction = (moveToDest - transform.position).normalized;
             transform.GetComponent<Rigidbody>().velocity = direction * speed;
             
             
         }
         else if(transform.position == destination) 
         {
             transform.GetComponent<Rigidbody>().velocity = Vector3.zero;
             
         }
     }
     
 }
 

}

Then here's the Camera Control Script:

using UnityEngine;

using System.Collections;

using System.Collections.Generic;

public class CameraControl : MonoBehaviour {

 public Texture2D selectionHighlight = null;

 public Rect selection = new Rect(0, 0, 0, 0);

 private Vector3 startClick = -Vector3.one;

 public float speed;

 private static Vector3 moveToDestination = Vector3.zero;

 private static List<string> passables = new List<string>() { "Floor" };

 public RaycastHit hitTarget;

 public GameObject target;

 public GameObject targetInstance;
 
 public float ZoomAmount = 0; //With Positive and negative values

 public float MaxToClamp = 10;

 public float ROTSpeed = 10;
 
 void Update () {
     

     ZoomAmount += Input.GetAxis("Mouse ScrollWheel");

     ZoomAmount = Mathf.Clamp(ZoomAmount, -MaxToClamp, MaxToClamp);

     var translate = Mathf.Min(Mathf.Abs(Input.GetAxis("Mouse ScrollWheel")), MaxToClamp -Mathf.Abs(ZoomAmount));

     gameObject.transform.Translate(0, 0, translate * ROTSpeed * Mathf.Sign(Input.GetAxis("Mouse ScrollWheel")));
   
     GameObject unit = GameObject.FindGameObjectWithTag("SelectableUnit");

     if (Input.GetMouseButtonUp(1) && unit.GetComponent<Unit>().selected)
     {
         Destroy(targetInstance);

         targetInstance = Instantiate(target, unit.GetComponent<Unit>().destination, Quaternion.identity) as GameObject;
         
     }
     else if(Input.GetMouseButtonDown(0))
     {
         
         Destroy(targetInstance);
     }
     Cleanup();
     CheckCamera();
     if (Input.GetKey(KeyCode.W))
     {
         transform.Translate(Vector3.up * speed);
     }
     if (Input.GetKey(KeyCode.S))
     {
         transform.Translate(Vector3.down * speed);
     }
     if (Input.GetKey(KeyCode.A))
     {
         transform.Translate(Vector3.left * speed);
     }
     if (Input.GetKey(KeyCode.D))
     {
         transform.Translate(Vector3.right * speed);
     }

 }
 private void OnGUI()
 {
     if(startClick != -Vector3.one)
     {
         GUI.color = new Color(1, 1, 1, 0.5f);
         GUI.DrawTexture(selection, selectionHighlight);
     }
 }
 void CheckCamera()
     {
     if (Input.GetMouseButtonDown(0))
     {
         startClick = Input.mousePosition;

     }
     else if (Input.GetMouseButtonUp(0))
     {
         
         startClick = -Vector3.one;
     }
     if (Input.GetMouseButton(0))
     {

         selection = new Rect(startClick.x, InvertMouseY(startClick.y), Input.mousePosition.x - startClick.x, InvertMouseY(Input.mousePosition.y) - InvertMouseY(startClick.y));

         if (selection.width < 0)
         {
             selection.x += selection.width;
             selection.width = -selection.width;
         }
         if (selection.height < 0)
         {
             selection.y += selection.height;
             selection.height = -selection.height;
         }
     }
  }
 public float InvertMouseY(float y)
 {
     return Screen.height - y;
 }
 private void Cleanup()
 {
     if (!Input.GetMouseButtonUp(1))
     {
         moveToDestination = Vector3.zero;
         
         
     }
 }
 public Vector3 GetDestination()
 {
     if(moveToDestination == Vector3.zero)
     {
         RaycastHit hit;
         Ray r = Camera.main.ScreenPointToRay(Input.mousePosition);
         if(Physics.Raycast(r, out hit))
         {
             
             while (!passables.Contains(hit.transform.gameObject.name))
             {
                 if (!Physics.Raycast(hit.point + r.direction * 0.1f, r.direction, out hit))
                 {
                     
                     break;
                     
                 }
                     
             }
             Debug.Log("Assigned Move Destination");
             moveToDestination = hit.point;
             hitTarget = hit;
         }
             
     }
     
     return moveToDestination;
 }

}

Most of the selection code is from a tutorial on Youtube. Unfortunately I don't know where I went wrong so I don't know where to revert back to.

Comment
Add comment · Show 1
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 UncleGubbsy · Apr 01, 2016 at 11:18 PM 0
Share

@Happy Zomby The error was on line of the unit script with (vector3 campos = Camera.....). I sorta fixed it but the unit doesnt get selected. So not really. I think the error was that I put camera.main. I dont really know though. I renamed the camera in the hierarchy playerCam, if that has anything to do with it. As you can see underneath where the error is , selected = camControl.selection.Contains(camPos); that should become true and it's not so my unit's color doesn't change. Unity although doesnt give me an error.

2 Replies

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

Answer by Happy-Zomby · Apr 01, 2016 at 11:05 PM

Hi, When you get the error, you should also see which script and line, can you post it here?

Comment
Add comment · 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
0

Answer by UncleGubbsy · Apr 02, 2016 at 12:38 AM

I'm sorry to bother you with this question but, I solved the problem. Instead of referencing the player cam in the Inspector, I found it in code. Thanks for your concern though. Have a nice day.

Comment
Add comment · 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

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

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

Related Questions

Vector3 is increasing infinitely instead of capping at the if statement limit,Doing a smooth move for a camera but it continues to pan even though it reaches it's x and y offset 0 Answers

How I can make my camera keep a distance between two objects? 0 Answers

Needing a hand circumventing a "Missing Reference Exception". Any help sincerely appreciated! 1 Answer

Teleport camera to where is pointing at 3 Answers

Camera looking at the player at an angle 1 Answer


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