(Unity3d) **nullreferenceexception** thrown when *instance.lockpos()* is called; plus how to make an object maintain its starting rotation - unity3d

I've run up against a little problem, and I can't seem to find the solution.
I'm putting together a game in Unity3d that includes these gauntlets that can control the environment, i.e. raise downed bridges, open doors, etc. First, I'm just trying to allow the gauntlets to access objects from afar, and I found a telekinesis tutorial online that I am trying to pseudo-repurpose.
This script is supposed to raise a bridge (hence BridgeMover) until it hits a specific position and then lock it in place. Unfortunately, I'm throwing a null reference exception when the bridge hits the "lockbox" object, which is a small cube on the edge of the bridge ramp leading up to the bridge. I know this is simple, but I can't quite conceptualize it, and I've just been staring at it for a few days. This is my big issue, and any help would be appreciated.
In other news, I'm also trying to have the bridge rise as a flat plane instead of following the rotation of the cast ray, and would appreciate any advice about that as well, but I would understand if y'all want me to suffer through on that one. I'm pretty sure it has something to do with the Lerp, which I still don't really understand.
In the hierarchy, I have a handhold object, which is a child of the gauntlet object, which is a child of the camera, which is a child of the player. So
Player
Camera
Gauntlet
handHold
The lockbox object is a child of the ramp that it's attached to. So:
Ramp
Lockbox
Also, this isn't a criticism of the tutorial; I'm just swimming slightly out of my depths here. The tutorial, for those interested, can be found here.
public class HandController : MonoBehaviour
{
public static BridgeMover instance;
public Camera mainCamera;
Rigidbody rbOfHeldObject;
GameObject heldObject;
public GameObject handHold;
bool holdsObject = false;
float interactionDistance = 100;
float slideSpeed = 2;
Quaternion currentRotation;
private void Start()
{
mainCamera.GetComponent<Camera>();
}
private void Update()
{
//HandMove();
if (Input.GetMouseButtonDown(0) && !holdsObject)
Raycast();
if (Input.GetMouseButtonUp(0) && holdsObject)
ReleaseObject();
if (holdsObject && instance.LockPos())
LockObject();
if(holdsObject)
{
if (CheckDistance() >= 0.1f)
MoveObjectToPosition();
}
}
private void Raycast()
{
Ray ray = mainCamera.ViewportPointToRay(new Vector3(0.5f, 0.5f, 0));
RaycastHit hit;
if (Physics.Raycast(ray, out hit, interactionDistance))
{
if(hit.collider.CompareTag("Bridge"))
{
heldObject = hit.collider.gameObject;
heldObject.transform.SetParent(handHold.transform);
holdsObject = true;
rbOfHeldObject = heldObject.GetComponent<Rigidbody>();
rbOfHeldObject.constraints = RigidbodyConstraints.FreezeAll;
}
}
}
public float CheckDistance()
{
return Vector3.Distance(heldObject.transform.position, handHold.transform.position);
}
private void MoveObjectToPosition()
{
currentRotation = heldObject.transform.rotation;
heldObject.transform.position = Vector3.Lerp(heldObject.transform.position,
handHold.transform.position, slideSpeed * Time.deltaTime);
heldObject.transform.rotation = currentRotation;
}
private void ReleaseObject()
{
rbOfHeldObject.constraints = RigidbodyConstraints.None;
heldObject.transform.parent = null;
heldObject = null;
holdsObject = false;
}
private void LockObject()
{
rbOfHeldObject.useGravity = false;
heldObject.transform.parent = null;
heldObject = null;
holdsObject = false;
}
}
And then in the BridgeMover class:
public class BridgeMover : MonoBehaviour
{
public static BridgeMover instance;
bool lockPos = false;
private void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.tag == "LockBox")
{
lockPos = true;
}
}
public bool LockPos()
{
return lockPos;
}
}
Any help would be appreciated. Also, if any further information is needed, let me know and I'll add it.

It looks like you're trying to implement a singleton instance for BridgeMover. Unfortunately, you don't have your instance defined. Here's the standard Unity method for creating a singleton implemented in your BridgeMover code:
public class BridgeMover : MonoBehaviour
{
public static BridgeMover instance;
bool lockPos = false;
void Awake()
{
if(instance == null)
{
instance = this;
}
else
{
Destroy(this.gameObject);
}
}
private void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.tag == "LockBox")
{
lockPos = true;
}
}
public bool LockPos()
{
return lockPos;
}
}
Additionally, in your HandController class you should not declare a static BridgeMover. Instead, reference the static instance properly:
public static BridgeMover instance;
if (holdsObject && BridgeMover.instance.LockPos())

Related

How can I check FindGameObjectsWithTag during runtime?

I'm having troubles with my enemy NavMeshAgent AI, First it searches for GameObjects tagged with "Defenses", then I set the first destination based in how close the defense is by my enemy, when the defense is destroyed, the array value is increased by one and it changes the destination to the next defense.
My problem is, my player can create (Instance) new defenses during the game, when all my defenses are destroyed, my enemy turns crazy, so, i need a way to add those new defenses to my array, below is my enemy script.
public class NavMeshEnemy : MonoBehaviour
{
[Header("References", order = 0)]
[SerializeField] NavMeshAgent enemyAgent;
[Space(10)]
[SerializeField] GameObject[] destinations;
[Space(10)]
[SerializeField] float distanceObjects;
[SerializeField] int arrayElements = 0;
void Awake()
{
enemyAgent = GetComponent<NavMeshAgent>();
destinations = GameObject.FindGameObjectsWithTag("Defenses");
}
void Start()
{
destinations = destinations.OrderBy((d) => (d.transform.position - this.transform.position).sqrMagnitude).ToArray();
}
void Update()
{
distanceObjects = Vector3.Distance(this.transform.position, destinations[arrayElements].transform.position);
enemyAgent.destination = destinations[arrayElements].transform.position;
CheckArray();
}
void CheckArray()
{
if (destinations[arrayElements].gameObject.activeInHierarchy == false)
{
if (arrayElements < destinations.Length - 1)
{
arrayElements++;
enemyAgent.destination = destinations[arrayElements].transform.position;
}
else
{
arrayElements = 0;
}
}
}
}
Thank you for reading! :)
I would rather implement a component on your prefab with a static list like e.g.
public class NavMeshDestination : MonoBehaviour
{
public static HashSet<Transform> existingDestinations = new HashSet<Transform>();
private void Awake()
{
if(!existingDestinations.Contains(this)) existingDestinations.Add(this);
}
private void OnDestroy()
{
if(existingDestinations.Contains(this)) existingDestinations.Remove(this);
}
}
And then don't even go by tag but simply do
public class NavMeshEnemy : MonoBehaviour
{
[Header("References", order = 0)]
[SerializeField] NavMeshAgent enemyAgent;
[Space(10)]
[SerializeField] float distanceObjects;
[SerializeField] int arrayElements = 0;
void Awake()
{
if(!enemyAgent) enemyAgent = GetComponent<NavMeshAgent>();
}
void Update()
{
destinations = NavMeshDestination.existingDestinations.OrderBy(d => (d.transform.position - this.transform.position).sqrMagnitude).ToArray();
distanceObjects = Vector3.Distance(this.transform.position, destinations[arrayElements].transform.position);
enemyAgent.destination = destinations[arrayElements].transform.position;
CheckArray();
}
void CheckArray()
{
if (destinations[arrayElements].gameObject.activeInHierarchy == false)
{
if (arrayElements < destinations.Length - 1)
{
arrayElements++;
enemyAgent.destination = destinos[arrayElements].transform.position;
}
else
{
arrayElements = 0;
}
}
}
}
Note however that this still won't fix your issues with no available destination. So you probably should stop the execution if the array is empty like
if(NavMeshDestination.existingDestinations.Count == 0)
{
// Stop the enemyAgent
return;
}

Instantiate making multiple clones

I'm making a building system in my game and the basic mechanic works but for some reason whenever I place down the object it adds incrementally more clones with every placement. The first placement creates one clone, the second placement creates two clones, the third, three clones, and so on. I fixed it for one object by moving it out of an empty game object that had all the building prefabs in the hierarchy but it only works for that.
using UnityEngine;
using UnityEngine.AI;
public class GroundPlacementController : MonoBehaviour
{
[SerializeField]
private GameObject placeableObjectPrefab;
[SerializeField]
private KeyCode newObjectHotkey = KeyCode.A;
public NavMeshObstacle nav;
private GameObject currentPlaceableObject;
private float mouseWheelRotation;
private void Update()
{
HandleNewObjectHotkey();
nav = GetComponent<NavMeshObstacle>();
if (currentPlaceableObject != null)
{
MoveCurrentObjectToMouse();
RotateFromMouseWheel();
ReleaseIfClicked();
}
}
private void HandleNewObjectHotkey()
{
if (Input.GetKeyDown(newObjectHotkey))
{
if (currentPlaceableObject != null)
{
Destroy(currentPlaceableObject);
}
else
{
currentPlaceableObject = Instantiate(placeableObjectPrefab);
}
}
}
private void MoveCurrentObjectToMouse()
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hitInfo;
if (Physics.Raycast(ray, out hitInfo))
{
currentPlaceableObject.transform.position = hitInfo.point;
currentPlaceableObject.transform.rotation = Quaternion.FromToRotation(Vector3.up, hitInfo.normal);
currentPlaceableObject.GetComponent<NavMeshObstacle>().enabled = false;
}
}
private void RotateFromMouseWheel()
{
Debug.Log(Input.mouseScrollDelta);
mouseWheelRotation += Input.mouseScrollDelta.y;
currentPlaceableObject.transform.Rotate(Vector3.up, mouseWheelRotation * 90f);
}
private void ReleaseIfClicked()
{
if (Input.GetMouseButtonDown(0))
{
currentPlaceableObject.GetComponent<NavMeshObstacle>().enabled = true;
print("disabled");
currentPlaceableObject = null;
print("removed prefab");
}
}
}

How do I stop my objects from going through walls when I pick them up and drop them down?

When I pick an object up I can glitch it through the map making it fall out of the world. This happens when I pick it up and drop the object half way through the floor. The outcome I receive is not what I was expecting what can I do to fix this. Also yes the colliders and rigidbody's are setup correctly.
public GameObject PressEtoInteractText;
public bool pickup, inrange;
public Collider Playercol;
public Vector3 guide;
private GameObject temp;
private Rigidbody rb;
void Update()
{
if (Input.GetKeyDown(KeyCode.E) && inrange == true)
{
PressEtoInteractText.SetActive(false);
pickup = true;
}
if (Input.GetMouseButtonDown(0) && pickup == true)
{
pickup = false;
Playercol.isTrigger = true;
}
UpdatePickUpFollow();
}
void OnTriggerEnter(Collider other)
{
if (other.gameObject.tag == "Interact")
{
PressEtoInteractText.SetActive(true);
temp = other.gameObject;
inrange = true;
}
if (other.gameObject.tag == "Interact" && temp.transform.position == guide)
{
return;
}
}
void OnTriggerExit(Collider other)
{
if (other.gameObject.name != "Interact")
{
PressEtoInteractText.SetActive(false);
inrange = false;
}
}
public void PickUp()
{
rb = temp.GetComponent<Rigidbody>();
rb.MovePosition(transform.position += guide);
Playercol.isTrigger = false;
}
public void UpdatePickUpFollow()
{
if (pickup == true)
{
PickUp();
}
}
If you deactivate the objects collider with tempCol.enabled = false; it will not register any collisions and you can just push or pull it through walls all day. Make sure to remove that from your script.
Also, using MoveTowards can cause issues with collision detection. In my experience it is best to use AddForce or MovePosition to move the Rigidbody component instead of modifying the Transform directly. Try Rigidbody.MovePosition(Vector3 position). Maybe this works better for you.
Here is a link to the documentation page. It's basically your exact use case:
Rigidbody.MoveTowards Unity Docs
(Hint: notice how they use FixedUpdate instead of regular Update. You should also always do this when working with Rigidbodies because it is synced with physics updates.)
//Edit:
This is a little implementation of your code in a cleaner and hopefully correct way. I have not tested this and there is propably something I missed. Use this as a reference to build your own working solution.
//set in inspector
public Transform guidePosition;
public GameObject pressEToInteractText;
private bool isObjectInRange;
private GameObject inRangeObject;
private bool isObjectPickedUp;
private Rigidbody pickedUpRigidBody;
/* check if button is pressed.
* If no object is in hand -> pick up;
* if object is in hand -> drop;
*/
private void FixedUpdate()
{
if (Input.GetKeyDown(KeyCode.E) && isObjectInRange && !isObjectPickedUp)
{
PickupObject();
}
if(Input.GetKeyDown(KeyCode.E) && isObjectPickedUp)
{
DropObject();
}
if (isObjectPickedUp)
{
PickedUpObjectFollow();
}
}
//save references
private void PickupObject()
{
pickedUpRigidBody = inRangeObject.GetComponent<Rigidbody>();
isObjectPickedUp = true;
}
//remove saved references
private void DropObject()
{
isObjectPickedUp = false;
pickedUpRigidBody = null;
}
//move position to guidePosition
private void PickedUpObjectFollow()
{
pickedUpRigidBody.MovePosition(guidePosition.position);
}
private void OnTriggerEnter(Collider other)
{
if (other.CompareTag("Interact") && !isObjectPickedUp)
{
pressEToInteractText.SetActive(true);
isObjectInRange = true;
inRangeObject = other.gameObject;
}
}
private void OnTriggerExit(Collider other)
{
if(other.CompareTag("Interact") && other.gameObject == inRangeObject)
{
pressEToInteractText.SetActive(false);
isObjectInRange = false;
inRangeObject = null;
}
}
You could do multiple things. Unfortunately the way VR tracks the hands and there is no way to stop ur hands in real life your hand will defy the physics in the application.
I would either make the item drop from the users hand when it gets close enough to a certain area that you dont want it going through.
Or you can make it that if it does pass through a space you can report the item right above the floor it went through.
Ive looked long and hard for an answer to this and it doesnt seem like it can at this point be blocked from moving through if its currently grabbed.

Unity Game Object Controlled by real light spot on the wall

We have got various controllers developed for moving gameobjects. I have used magnetometer/gyro sensor to move game object using MUVSlide through following code:
using UnityEngine;
using ForestIndieGames.Muvslide;
public class Connection : MonoBehaviour {
private static bool created = false;
private bool newInputAvailable;
private MuvslideConnection muvslideConn;
private void Awake()
{
if (!created)
{
DontDestroyOnLoad(this.gameObject);
created = true;
muvslideConn = new MuvslideConnection();
}
}
private void OnApplicationQuit()
{
if (muvslideConn != null)
muvslideConn.Close();
}
private void Update()
{
if (muvslideConn.GetInputManager().IsNewMotionAvailable())
newInputAvailable = true;
}
public bool IsNewInputAvailable()
{
bool result = newInputAvailable;
newInputAvailable = false;
return result;
}
public Vector3 GetAngles()
{
float[] angles = muvslideConn.GetInputManager().GetOrientationDegrees();
return new Vector3(angles[0], angles[1], angles[2]);
}
}
What I am trying to achieve is to move a gameobject by a real light spot on the wall. The spot is on the wall and fed through a camera. When the spot moves I want game object to follow exactly. The light spot can be a specific color or IR or UV etc.
Any leads please

Unity3d Transform.LookAt also changes position

Title says it all.
I provided an NPC with the following script, which should make him look and bark at the player.
When the player comes into the NPC's reach, however, the NPC starts walking towards the player instead of just facing him.
Any thoughts?
using UnityEngine;
public class Barking : MonoBehaviour {
public AudioSource barkingAudio;
private GameObject player;
private bool barking;
void Start () {
player = GameObject.FindGameObjectWithTag("Player");
barking = false;
}
void Update () {
if (barking)
lookAtPlayer();
}
private void lookAtPlayer()
{
transform.LookAt(player.transform.position, Vector3.up);
}
private void OnTriggerEnter(Collider other)
{
if (other.gameObject == player)
{
barking = true;
barkingAudio.mute = false;
barkingAudio.Play();
}
}
private void OnTriggerExit(Collider other)
{
if (other.gameObject == player) {
barking = false;
barkingAudio.mute = true;
barkingAudio.Stop();
}
}
}
Since I was using a Rigidbody and rotating the transform manually, there was some unexpected behaviour.
I found some code online which I could replace the Transform.LookAt method with:
var qTo = Quaternion.LookRotation(player.transform.position - transform.position);
qTo = Quaternion.Slerp(transform.rotation, qTo, 10 * Time.deltaTime);
GetComponent<Rigidbody>().MoveRotation(qTo);
This fixed my problem!