Unity Object Reference Not Set To An Instance Of An Object [duplicate] - c#

This question already has answers here:
What is a NullReferenceException, and how do I fix it?
(27 answers)
Closed 9 months ago.
I have a stanza of code that sees if a collider is intersecting with another collider, but it's giving me an error. I've pasted the error below.
NullReferenceException: Object reference not set to an instance of an object
Portal.LateUpdate () (at Assets/Scripts/Portal.cs:112)
I have no idea why this is happening. Thanks for the help in advance! <3
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Portal : MonoBehaviour
{
//Portal Camera Positioning
public Portal linkedPortal;
Camera portalCam;
Camera playerCam;
//Portal Sceen Texture and Clipping Protection
[HideInInspector]
public PortalScreen portalScreen;
MeshRenderer screenRen;
RenderTexture viewTexture;
public PortalTraveller activeTraveller;
public Collider travellerCollider;
List<PortalTraveller> travellersInPortalColliders;
//Travellers
[HideInInspector]
public List<PortalTraveller> trackedTravellers;
Vector3 desiredCameraRotation;
Vector3 desiredCameraPosition;
Transform globalRuler;
public PortalTraveller[] travellersToAdd;
[HideInInspector]
public float travellerDistance;
//Debug
[HideInInspector]
public float dstToPlayer;
void Awake()
{
playerCam = Camera.main;
portalCam = linkedPortal.GetComponentInChildren<Camera>();
portalScreen = GetComponentInChildren<PortalScreen>();
screenRen = portalScreen.GetComponent<MeshRenderer>();
globalRuler = GameObject.FindWithTag("GlobalRuler").transform;
trackedTravellers = new List<PortalTraveller>();
travellersToAdd = GameObject.FindObjectsOfType<PortalTraveller>();
foreach (PortalTraveller traveller in travellersToAdd)
{
if (!trackedTravellers.Contains(traveller))
{
trackedTravellers.Add(traveller);
}
}
}
void Update()
{
Render();
if (activeTraveller != null)
travellerCollider = activeTraveller.GetComponent<Collider>();
}
static bool VisibleFromCamera(Renderer renderer, Camera camera)
{
Plane[] frustumPlanes = GeometryUtility.CalculateFrustumPlanes(camera);
return GeometryUtility.TestPlanesAABB(frustumPlanes, renderer.bounds);
}
void Render()
{
if (!VisibleFromCamera(screenRen, playerCam))
{
return;
}
desiredCameraPosition = transform.position - (playerCam.transform.position - linkedPortal.portalScreen.screenPos);
desiredCameraPosition.y = playerCam.transform.position.y;
desiredCameraRotation = playerCam.transform.eulerAngles + new Vector3 (0f, 180f, 0f);
portalCam.transform.position = desiredCameraPosition;
portalCam.transform.eulerAngles = desiredCameraRotation;
if (viewTexture == null || viewTexture.width != Screen.width || viewTexture.height != Screen.height)
{
if (viewTexture != null)
{
viewTexture.Release();
}
screenRen.shadowCastingMode = UnityEngine.Rendering.ShadowCastingMode.ShadowsOnly;
viewTexture = new RenderTexture (Screen.width, Screen.height, 0);
portalCam.targetTexture = viewTexture;
screenRen.material.SetTexture("_MainTex", viewTexture);
screenRen.shadowCastingMode = UnityEngine.Rendering.ShadowCastingMode.On;
}
}
void LateUpdate()
{
HandleTravellers();
foreach (PortalTraveller traveller in travellersToAdd)
{
if (!trackedTravellers.Contains(traveller))
{
trackedTravellers.Add(traveller);
}
}
foreach (PortalTraveller traveller in trackedTravellers)
{
if (!travellersInPortalColliders.Contains(traveller))
{
traveller.hasTeleported = false;
}
}
}
void HandleTravellers()
{
for (int i = 0; i < trackedTravellers.Count; i++)
{
PortalTraveller traveller = trackedTravellers[i];
Transform travellerT = trackedTravellers[i].transform;
Vector3 toTraveller = traveller.transform.position - transform.position;
int portalSide = System.Math.Sign(Vector3.Dot(toTraveller, transform.right));
int portalSideOld = System.Math.Sign(Vector3.Dot(traveller.previousTravellerVector, transform.right));
travellerDistance = Mathf.Abs(Vector3.Distance(transform.position, travellerT.position));
activeTraveller = traveller;
if (travellerDistance > linkedPortal.travellerDistance)
{
break;
}
if (portalSide != portalSideOld && travellerDistance < globalRuler.transform.lossyScale.z / 2)
{
var positionOld = travellerT.position;
var rotationOld = travellerT.rotation;
Quaternion rot = Quaternion.Euler(desiredCameraRotation.x, desiredCameraRotation.y, 0f);
traveller.Teleport(transform, linkedPortal.transform, travellerT);
traveller.previousTravellerVector = toTraveller;
trackedTravellers.RemoveAt(i);
i--;
}
else
{
traveller.previousTravellerVector = toTraveller;
dstToPlayer = Mathf.Abs(Vector3.Distance(transform.position, travellerT.position));
}
}
}
void OnValidate()
{
if (linkedPortal != null)
{
linkedPortal.linkedPortal = this;
}
travellersToAdd = GameObject.FindObjectsOfType<PortalTraveller>();
}
void OnTriggerEnter(Collider other)
{
if (other.GetComponent<PortalTraveller>() != null)
{
var travellerSharedCollider = other.GetComponent<PortalTraveller>();
if (!travellersInPortalColliders.Contains(travellerSharedCollider))
{
travellersInPortalColliders.Add(travellerSharedCollider);
linkedPortal.travellersInPortalColliders.Add(travellerSharedCollider);
}
}
}
void OnTriggerExit(Collider other)
{
if (other.GetComponent<PortalTraveller>() != null)
{
var travellerSharedCollider = other.GetComponent<PortalTraveller>();
if (travellersInPortalColliders.Contains(travellerSharedCollider))
{
travellersInPortalColliders.Remove(travellerSharedCollider);
linkedPortal.travellersInPortalColliders.Remove(travellerSharedCollider);
}
}
}
}

The reason for the NullReferenceException is because the variable List travellersInPortalColliders is never initialized. You need to somewhere in your code put:
travellersInPortalColliders = new List<PortalTraveller>();
I would suggest somewhere in your awake function. For Example:
void Awake()
{
playerCam = Camera.main;
portalCam = linkedPortal.GetComponentInChildren<Camera>();
portalScreen = GetComponentInChildren<PortalScreen>();
screenRen = portalScreen.GetComponent<MeshRenderer>();
globalRuler = GameObject.FindWithTag("GlobalRuler").transform;
trackedTravellers = new List<PortalTraveller>();
travellersToAdd = GameObject.FindObjectsOfType<PortalTraveller>();
travellersInPortalColliders = new List<PortalTraveller>();
foreach (PortalTraveller traveller in travellersToAdd)
{
if (!trackedTravellers.Contains(traveller))
{
trackedTravellers.Add(traveller);
}
}
}

Related

Values Getting Skewed When Passed Between Scripts Unity

I'm working on a portal system and am up to making the traveller go through the portal. For this, I just set the traveller's position to the position of the other portal. When I do this, however, the Z rotation of the camera gets skewed, and they are teleported slightly higher than they should. Any help is welcome <3
Portal Script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Portal : MonoBehaviour
{
//Portal Camera Positioning
public Portal linkedPortal;
private Camera portalCam;
private Camera playerCam;
//Portal Sceen Texture
private PortalScreen portalScreen;
private MeshRenderer screenRen;
private RenderTexture viewTexture;
//Travellers
//[SerializableField]
List<PortalTraveller> trackedTravellers;
public Vector3 desiredCameraRotation;
public Vector3 desiredCameraPosition;
private Transform globalRuler;
PortalTraveller[] travellersToAdd;
[HideInInspector]
public float travellerDistance;
//Debug
[HideInInspector]
public float dstToPlayer;
void Awake()
{
playerCam = Camera.main;
portalCam = linkedPortal.GetComponentInChildren<Camera>();
portalScreen = GetComponentInChildren<PortalScreen>();
screenRen = portalScreen.GetComponent<MeshRenderer>();
globalRuler = GameObject.FindWithTag("GlobalRuler").transform;
trackedTravellers = new List<PortalTraveller>();
travellersToAdd = GameObject.FindObjectsOfType<PortalTraveller>();
foreach (PortalTraveller traveller in travellersToAdd)
{
if (!trackedTravellers.Contains(traveller))
{
trackedTravellers.Add(traveller);
}
}
}
void Update()
{
Render();
}
static bool VisibleFromCamera(Renderer renderer, Camera camera)
{
Plane[] frustumPlanes = GeometryUtility.CalculateFrustumPlanes(camera);
return GeometryUtility.TestPlanesAABB(frustumPlanes, renderer.bounds);
}
void Render()
{
if (!VisibleFromCamera(screenRen, playerCam))
{
return;
}
desiredCameraPosition = transform.position - (playerCam.transform.position - linkedPortal.portalScreen.screenPos);
desiredCameraPosition.y = playerCam.transform.position.y;
desiredCameraRotation = playerCam.transform.eulerAngles + new Vector3 (0f, 180f, 0f);
portalCam.transform.position = desiredCameraPosition;
portalCam.transform.eulerAngles = desiredCameraRotation;
if (viewTexture == null || viewTexture.width != Screen.width || viewTexture.height != Screen.height)
{
if (viewTexture != null)
{
viewTexture.Release();
}
viewTexture = new RenderTexture (Screen.width, Screen.height, 0);
portalCam.targetTexture = viewTexture;
screenRen.material.SetTexture("_MainTex", viewTexture);
}
}
void LateUpdate()
{
HandleTravellers();
}
void HandleTravellers()
{
for (int i = 0; i < trackedTravellers.Count; i++)
{
PortalTraveller traveller = trackedTravellers[i];
Transform travellerT = trackedTravellers[i].transform;
Vector3 toTraveller = traveller.transform.position - transform.position;
int portalSide = System.Math.Sign(Vector3.Dot(toTraveller, transform.right));
int portalSideOld = System.Math.Sign(Vector3.Dot(traveller.previousTravellerVector, transform.right));
travellerDistance = Mathf.Abs(Vector3.Distance(transform.position, travellerT.position));
//Debug.Log("Handled");
if (travellerDistance > linkedPortal.travellerDistance)
{
break;
}
if (portalSide < portalSideOld && travellerDistance < globalRuler.transform.lossyScale.z / 2 )
{
//Debug.Log(travellerDistance);
var positionOld = travellerT.position;
var rotationOld = travellerT.rotation;
Quaternion rot = Quaternion.Euler(desiredCameraRotation.x, desiredCameraRotation.y, 0f);
traveller.Teleport(transform, linkedPortal.transform, desiredCamerxaPosition, rot);
traveller.previousTravellerVector = toTraveller;
//trackedTravellers.RemoveAt(i);
i--;
}
else
{
traveller.previousTravellerVector = toTraveller;
dstToPlayer = Mathf.Abs(Vector3.Distance(transform.position, travellerT.position));
}
}
}
void OnValidate()
{
if (linkedPortal != null)
{
linkedPortal.linkedPortal = this;
}
travellersToAdd = GameObject.FindObjectsOfType<PortalTraveller>();
}
}
Traveller Script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PortalTraveller : MonoBehaviour
{
public Vector3 previousTravellerVector;
public void Teleport(Transform originalPortal, Transform linkedPortal, Vector3 pos, Quaternion rot)
{
transform.position = pos;
transform.rotation = rot;
}
}

Unity C# script variable not changing in game when changed in script

Very new to Unity, I'm making a tactical RPG game similar to Fire Emblem. I've followed a tutorial on movement from this website and followed everything he did.
The player character can only move 5 tiles in the directions +z, -z, +x, -x on a 12x12 tile grid, however I want it to move 6. In the TacticsMove.cs script, there is this variable: public int move = 5; with the amount of the variable being how many tiles the player moves. I've changed the variable to public int move = 6; and saved the script, but everything is the exact same as before.
I want public int move = 6 to allow the player to move up to 6 tiles instead of 5. There are no comments on the tutorial or youtube videos of this happening so the next place im coming to is here. My code is below, I don't know if all of this is relevant. If you need me to add/remove something, tell me.
TacticsMove.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TacticsMove : MonoBehaviour
{
public bool turn = false;
List<Tile> selectableTiles = new List<Tile>();
GameObject[] tiles;
Stack<Tile> path = new Stack<Tile>();
Tile currentTile;
public bool moving = false;
public int move = 6;
public float jumpHeight = 2;
public float moveSpeed = 2;
public float jumpVelocity = 4.5f;
Vector3 velocity = new Vector3();
Vector3 heading = new Vector3();
float halfHeight = 0;
bool fallingDown = false;
bool jumpingUp = false;
bool movingEdge = false;
Vector3 jumpTarget;
public Tile actualTargetTile;
protected void Init()
{
tiles = GameObject.FindGameObjectsWithTag("Tile");
halfHeight = GetComponent<Collider>().bounds.extents.y;
TurnManager.AddUnit(this);
}
public void GetCurrentTile()
{
currentTile = GetTargetTile(gameObject);
currentTile.current = true;
}
public Tile GetTargetTile(GameObject target)
{
RaycastHit hit;
Tile tile = null;
if (Physics.Raycast(target.transform.position, -Vector3.up, out hit, 1))
{
tile = hit.collider.GetComponent<Tile>();
}
return tile;
}
public void ComputeAdjacencyLists(float jumpHeight, Tile target)
{
//tiles = GameObject.FindGameObjectsWithTag("Tile");
foreach (GameObject tile in tiles)
{
Tile t = tile.GetComponent<Tile>();
t.FindNeighbors(jumpHeight, target);
}
}
public void FindSelectableTiles()
{
ComputeAdjacencyLists(jumpHeight, null);
GetCurrentTile();
Queue<Tile> process = new Queue<Tile>();
process.Enqueue(currentTile);
currentTile.visited = true;
//currentTile.parent = ?? leave as null
while (process.Count > 0)
{
Tile t = process.Dequeue();
selectableTiles.Add(t);
t.selectable = true;
if (t.distance < move)
{
foreach (Tile tile in t.adjacencyList)
{
if (!tile.visited)
{
tile.parent = t;
tile.visited = true;
tile.distance = 1 + t.distance;
process.Enqueue(tile);
}
}
}
}
}
public void MoveToTile(Tile tile)
{
path.Clear();
tile.target = true;
moving = true;
Tile next = tile;
while (next != null)
{
path.Push(next);
next = next.parent;
}
}
public void Move()
{
if (path.Count > 0)
{
Tile t = path.Peek();
Vector3 target = t.transform.position;
//Calculate the unit's position on top of the target tile
target.y += halfHeight + t.GetComponent<Collider>().bounds.extents.y;
if (Vector3.Distance(transform.position, target) >= 0.05f)
{
bool jump = transform.position.y != target.y;
if (jump)
{
Jump(target);
}
else
{
CalculateHeading(target);
SetHorizotalVelocity();
}
//Locomotion
transform.forward = heading;
transform.position += velocity * Time.deltaTime;
}
else
{
//Tile center reached
transform.position = target;
path.Pop();
}
}
else
{
RemoveSelectableTiles();
moving = false;
TurnManager.EndTurn();
}
}
protected void RemoveSelectableTiles()
{
if (currentTile != null)
{
currentTile.current = false;
currentTile = null;
}
foreach (Tile tile in selectableTiles)
{
tile.Reset();
}
selectableTiles.Clear();
}
void CalculateHeading(Vector3 target)
{
heading = target - transform.position;
heading.Normalize();
}
void SetHorizotalVelocity()
{
velocity = heading * moveSpeed;
}
void Jump(Vector3 target)
{
if (fallingDown)
{
FallDownward(target);
}
else if (jumpingUp)
{
JumpUpward(target);
}
else if (movingEdge)
{
MoveToEdge();
}
else
{
PrepareJump(target);
}
}
void PrepareJump(Vector3 target)
{
float targetY = target.y;
target.y = transform.position.y;
CalculateHeading(target);
if (transform.position.y > targetY)
{
fallingDown = false;
jumpingUp = false;
movingEdge = true;
jumpTarget = transform.position + (target - transform.position) / 2.0f;
}
else
{
fallingDown = false;
jumpingUp = true;
movingEdge = false;
velocity = heading * moveSpeed / 3.0f;
float difference = targetY - transform.position.y;
velocity.y = jumpVelocity * (0.5f + difference / 2.0f);
}
}
void FallDownward(Vector3 target)
{
velocity += Physics.gravity * Time.deltaTime;
if (transform.position.y <= target.y)
{
fallingDown = false;
jumpingUp = false;
movingEdge = false;
Vector3 p = transform.position;
p.y = target.y;
transform.position = p;
velocity = new Vector3();
}
}
void JumpUpward(Vector3 target)
{
velocity += Physics.gravity * Time.deltaTime;
if (transform.position.y > target.y)
{
jumpingUp = false;
fallingDown = true;
}
}
void MoveToEdge()
{
if (Vector3.Distance(transform.position, jumpTarget) >= 0.05f)
{
SetHorizotalVelocity();
}
else
{
movingEdge = false;
fallingDown = true;
velocity /= 5.0f;
velocity.y = 1.5f;
}
}
protected Tile FindLowestF(List<Tile> list)
{
Tile lowest = list[0];
foreach (Tile t in list)
{
if (t.f < lowest.f)
{
lowest = t;
}
}
list.Remove(lowest);
return lowest;
}
protected Tile FindEndTile(Tile t)
{
Stack<Tile> tempPath = new Stack<Tile>();
Tile next = t.parent;
while (next != null)
{
tempPath.Push(next);
next = next.parent;
}
if (tempPath.Count <= move)
{
return t.parent;
}
Tile endTile = null;
for (int i = 0; i <= move; i++)
{
endTile = tempPath.Pop();
}
return endTile;
}
protected void FindPath(Tile target)
{
ComputeAdjacencyLists(jumpHeight, target);
GetCurrentTile();
List<Tile> openList = new List<Tile>();
List<Tile> closedList = new List<Tile>();
openList.Add(currentTile);
//currentTile.parent = ??
currentTile.h = Vector3.Distance(currentTile.transform.position, target.transform.position);
currentTile.f = currentTile.h;
while (openList.Count > 0)
{
Tile t = FindLowestF(openList);
closedList.Add(t);
if (t == target)
{
actualTargetTile = FindEndTile(t);
MoveToTile(actualTargetTile);
return;
}
foreach (Tile tile in t.adjacencyList)
{
if (closedList.Contains(tile))
{
//Do nothing, already processed
}
else if (openList.Contains(tile))
{
float tempG = t.g + Vector3.Distance(tile.transform.position, t.transform.position);
if (tempG < tile.g)
{
tile.parent = t;
tile.g = tempG;
tile.f = tile.g + tile.h;
}
}
else
{
tile.parent = t;
tile.g = t.g + Vector3.Distance(tile.transform.position, t.transform.position);
tile.h = Vector3.Distance(tile.transform.position, target.transform.position);
tile.f = tile.g + tile.h;
openList.Add(tile);
}
}
}
//todo - what do you do if there is no path to the target tile?
Debug.Log("Path not found");
}
public void BeginTurn()
{
turn = true;
}
public void EndTurn()
{
turn = false;
}
}
Tile.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Tile : MonoBehaviour
{
public bool walkable = true;
public bool current = false;
public bool target = false;
public bool selectable = false;
public List<Tile> adjacencyList = new List<Tile>();
//Needed BFS (breadth first search)
public bool visited = false;
public Tile parent = null;
public int distance = 0;
//For A*
public float f = 0;
public float g = 0;
public float h = 0;
// Use this for initialization
void Start ()
{
}
// Update is called once per frame
void Update ()
{
if (current)
{
GetComponent<Renderer>().material.color = Color.magenta;
}
else if (target)
{
GetComponent<Renderer>().material.color = Color.green;
}
else if (selectable)
{
GetComponent<Renderer>().material.color = Color.blue;
}
else
{
GetComponent<Renderer>().material.color = Color.white;
}
}
public void Reset()
{
adjacencyList.Clear();
current = false;
target = false;
selectable = false;
visited = false;
parent = null;
distance = 0;
f = g = h = 0;
}
public void FindNeighbors(float jumpHeight, Tile target)
{
Reset();
CheckTile(Vector3.forward, jumpHeight, target);
CheckTile(-Vector3.forward, jumpHeight, target);
CheckTile(Vector3.right, jumpHeight, target);
CheckTile(-Vector3.right, jumpHeight, target);
}
public void CheckTile(Vector3 direction, float jumpHeight, Tile target)
{
Vector3 halfExtents = new Vector3(0.25f, (1 + jumpHeight) / 2.0f, 0.25f);
Collider[] colliders = Physics.OverlapBox(transform.position + direction, halfExtents);
foreach (Collider item in colliders)
{
Tile tile = item.GetComponent<Tile>();
if (tile != null && tile.walkable)
{
RaycastHit hit;
if (!Physics.Raycast(tile.transform.position, Vector3.up, out hit, 1) || (tile == target))
{
adjacencyList.Add(tile);
}
}
}
}
}
PlayerMove.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMove : TacticsMove
{
// Use this for initialization
void Start ()
{
Init();
}
// Update is called once per frame
void Update ()
{
Debug.DrawRay(transform.position, transform.forward);
if (!turn)
{
return;
}
if (!moving)
{
FindSelectableTiles();
CheckMouse();
}
else
{
Move();
}
}
void CheckMouse()
{
if (Input.GetMouseButtonUp(0))
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit))
{
if (hit.collider.tag == "Tile")
{
Tile t = hit.collider.GetComponent<Tile>();
if (t.selectable)
{
MoveToTile(t);
}
}
}
}
}
}
Im using unity version 2019.3.12f1.
Any help is appreciated.
As Etienne de Martel hints, Unity saves your public variables in the scene along with all the other GameObject/Component data, and restores that data on load
Field initializers are the default values for new components, they do not set the values for existing components even with a recompile.
I've been through this a few times myself.
From what I know, variables that have been serialized (which means it is visible in the inspector using [serialize] or it is set to public) will always overwrite scripts. This means that the moment u save ur script after u have create a variable and before u start the program, it can only be adjusted using the inspector

Unity: My enemy projectile is being destroyed before ever leaving it's spawn location. What am i doing wrong?

Like the title says, the enemy projectiles are not launching. They are spawned and destroyed in the same place. They do not fire toward the target. Code in link:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MonkeyController : MonoBehaviour {
private const int MAX_INJURES = 1;
public float DurationMovement = 2f;
public Transform SpawnLocation;
public Transform[] PatrolPoints;
public GameObject MonkeyProjectile;
public Rigidbody2D Abby;
public float AttackDelay = 0.75f;
public float ProjectileSpeed = 1;
public int ProjectilesCount = 4;
public float activePosition;
public float passivePosition;
private List<GameObject> ProjectileList = new List<GameObject>();
private int PatrolIndex = 0;
private int injures = 0;
private bool canPatrol;
private Coroutine fireCoroutine;
private Coroutine patrolCoroutine;
private Coroutine movementCoroutine;
WaitForSeconds delay;
Vector2 AttackVector = Vector2.zero;
// Use this for initialization
private void Start () {
delay = new WaitForSeconds(AttackDelay);
InitializeProjectileList();
fireCoroutine = StartCoroutine(MonkeyFireProjectileBehaviour());
patrolCoroutine = StartCoroutine(PatrolBehaviour(PatrolPoints[PatrolIndex].position));
}
private IEnumerator MonkeyFireProjectileBehaviour()
{
yield return delay;
if (GameManager.GetInstance().gameState == GameState.GAME_OVER)
yield return null;
AttackVector = Abby.transform.position - (transform.position /*+ (Vector3)Abby.velocity/10f*/);
FireProjectile(AttackVector);
fireCoroutine = StartCoroutine(MonkeyFireProjectileBehaviour());
}
private IEnumerator PatrolBehaviour(Vector3 animationLocation)
{
canPatrol = true;
float distance = (transform.position - animationLocation).magnitude;
float duration = DurationMovement;
Vector3 startingPos = transform.position;
float t = 0;
while (t < 1 && canPatrol)
{
t += Time.deltaTime / duration;
transform.position = Vector3.Lerp(startingPos, animationLocation, t);
yield return null;
}
if (!canPatrol)
yield break;
transform.position = animationLocation;
IncrementMovementIndex();
patrolCoroutine = StartCoroutine(PatrolBehaviour(PatrolPoints[PatrolIndex].position));
yield return null;
}
private void IncrementMovementIndex()
{
PatrolIndex++;
if(PatrolIndex == PatrolPoints.Length)
{
PatrolIndex = 0;
}
}
private void InitializeProjectileList()
{
GameObject Projectile;
for (int i = 0; i < ProjectilesCount; i++)
{
Projectile = Instantiate(MonkeyProjectile);
Projectile.SetActive(false);
ProjectileList.Add(Projectile);
}
}
private void FireProjectile(Vector2 forceProjectile)
{
foreach (GameObject projectile in ProjectileList)
{
if (!projectile.activeInHierarchy)
{
projectile.transform.position = SpawnLocation.position;
projectile.SetActive(true);
projectile.GetComponent<TrailRenderer>().enabled = true;
projectile.GetComponent<Rigidbody2D>().bodyType = RigidbodyType2D.Dynamic;
projectile.GetComponent<Rigidbody2D>().AddForce(forceProjectile * (ProjectileSpeed + Random.Range(0, ProjectileSpeed/2)), ForceMode2D.Impulse);
break;
}
}
}
private IEnumerator DoMovementCoroutine()
{
yield return new WaitForSeconds (0.01F);
transform.localPosition = new Vector2(passivePosition, 0);
yield return AnimatorExecutive.AnimatePositionCoroutine (gameObject, new Vector2 (activePosition, 0), 5.0F);
fireCoroutine = StartCoroutine(MonkeyFireProjectileBehaviour());
patrolCoroutine = StartCoroutine(PatrolBehaviour(PatrolPoints[PatrolIndex].position));
}
private void OnCollisionEnter2D(Collision2D otherCollision)
{
if (otherCollision.gameObject.tag == "Projectile")
{
injures++;
if (injures >= MAX_INJURES)
{
injures = 0;
canPatrol = false;
GetComponent<AudioSource>().Play();
if(fireCoroutine != null) StopCoroutine (fireCoroutine);
if(patrolCoroutine != null) StopCoroutine (patrolCoroutine);
movementCoroutine = StartCoroutine (DoMovementCoroutine());
}
}
}
}
With the information your provided, I would say the problem you may be facing is the GameObject you pass in the inspector to get the SpawnLocation has got a collider and a script with a OnCollisionEnter2D, which detect the projectil when you instantiate it and destroy it.
However, you are not destroying the projectil inside this OnCollisionEnter2D.
private void OnCollisionEnter2D(Collision2D otherCollision)
{
if (otherCollision.gameObject.tag == "Projectile")
{
injures++;
if (injures >= MAX_INJURES)
{
injures = 0;
canPatrol = false;
GetComponent<AudioSource>().Play();
if(fireCoroutine != null) StopCoroutine (fireCoroutine);
if(patrolCoroutine != null) StopCoroutine (patrolCoroutine);
movementCoroutine = StartCoroutine (DoMovementCoroutine());
}
}
}
In the case you dont have in your code any line to destroy the projectil gameobject after a collision. The problem could be you are not reaching this line projectile.SetActive(true);
I will try to replicate your code and check what may be happening

Unity HTC VIVE Teleportation

I am developing for the HTC VIVE and am trying to create a teleportation script that would allow the user to teleport to a certain location (predefined by me) when the user grabs a certain object. I currently have a teleportation code that works like a normal teleporter where the user points at a location and the room is moved to that location. I have modified this so that no matter where the user is pointing they will always teleport to the specific location. This is a first step but I am really looking to trigger this teleportation when the user picks up a certain object, does anyone have any ideas on where to star or how to do this?
Here is the code for the modified teleporter:
namespace VRTK{
using UnityEngine;
using System.Collections;
public delegate void TeleportEventHandler(object sender, DestinationMarkerEventArgs e);
public class VRTK_BasicTeleport : MonoBehaviour
{
public float blinkTransitionSpeed = 0.6f;
[Range(0f, 32f)]
public float distanceBlinkDelay = 0f;
public bool headsetPositionCompensation = true;
public string ignoreTargetWithTagOrClass;
public bool limitToNavMesh = false;
public event TeleportEventHandler Teleporting;
public event TeleportEventHandler Teleported;
protected Transform eyeCamera;
protected bool adjustYForTerrain = false;
protected bool enableTeleport = true;
private float blinkPause = 0f;
private float fadeInTime = 0f;
private float maxBlinkTransitionSpeed = 1.5f;
private float maxBlinkDistance = 33f;
public void InitDestinationSetListener(GameObject markerMaker)
{
if (markerMaker)
{
foreach (var worldMarker in markerMaker.GetComponents<VRTK_DestinationMarker>())
{
worldMarker.DestinationMarkerSet += new DestinationMarkerEventHandler(DoTeleport);
worldMarker.SetInvalidTarget(ignoreTargetWithTagOrClass);
worldMarker.SetNavMeshCheck(limitToNavMesh);
worldMarker.SetHeadsetPositionCompensation(headsetPositionCompensation);
}
}
}
protected virtual void Start()
{
Utilities.SetPlayerObject(this.gameObject, VRTK_PlayerObject.ObjectTypes.CameraRig);
adjustYForTerrain = false;
eyeCamera = Utilities.AddCameraFade();
InitDestinationMarkerListeners();
InitHeadsetCollisionListener();
enableTeleport = true;
}
protected void OnTeleporting(object sender, DestinationMarkerEventArgs e)
{
if (Teleporting != null)
Teleporting(this, e);
}
protected void OnTeleported(object sender, DestinationMarkerEventArgs e)
{
if (Teleported != null)
Teleported(this, e);
}
protected virtual void Blink(float transitionSpeed)
{
fadeInTime = transitionSpeed;
SteamVR_Fade.Start(Color.black, 0);
Invoke("ReleaseBlink", blinkPause);
}
protected virtual bool ValidLocation(Transform target)
{
//If the target is one of the player objects or a UI Canvas then it's never a valid location
if(target.GetComponent<VRTK_PlayerObject>() || target.GetComponent<VRTK_UIGraphicRaycaster>())
{
return false;
}
bool validNavMeshLocation = false;
if (target)
{
NavMeshHit hit;
validNavMeshLocation = NavMesh.SamplePosition(target.position, out hit, 1.0f, NavMesh.AllAreas);
}
if (!limitToNavMesh)
{
validNavMeshLocation = true;
}
return (validNavMeshLocation && target && target.tag != ignoreTargetWithTagOrClass && target.GetComponent(ignoreTargetWithTagOrClass) == null);
}
protected virtual void DoTeleport(object sender, DestinationMarkerEventArgs e)
{
if (enableTeleport && ValidLocation(e.target) && e.enableTeleport)
{
OnTeleporting(sender, e);
Vector3 newPosition = GetNewPosition(e.destinationPosition, e.target);
CalculateBlinkDelay(blinkTransitionSpeed, newPosition);
Blink(blinkTransitionSpeed);
SetNewPosition(newPosition, e.target);
OnTeleported(sender, e);
}
}
protected virtual void SetNewPosition(Vector3 position, Transform target)
{
this.transform.position = CheckTerrainCollision(position, target);
}
protected virtual Vector3 GetNewPosition(Vector3 tipPosition, Transform target)
{
float newX = 0;
float newY = 17;
float newZ = 0;
return new Vector3(newX, newY, newZ);
}
protected Vector3 CheckTerrainCollision(Vector3 position, Transform target)
{
if (adjustYForTerrain && target.GetComponent<Terrain>())
{
var terrainHeight = Terrain.activeTerrain.SampleHeight(position);
position.y = (terrainHeight > position.y ? position.y : terrainHeight);
}
return position;
}
private void CalculateBlinkDelay(float blinkSpeed, Vector3 newPosition)
{
blinkPause = 0f;
if (distanceBlinkDelay > 0f)
{
float distance = Vector3.Distance(this.transform.position, newPosition);
blinkPause = Mathf.Clamp((distance * blinkTransitionSpeed) / (maxBlinkDistance - distanceBlinkDelay), 0, maxBlinkTransitionSpeed);
blinkPause = (blinkSpeed <= 0.25 ? 0f : blinkPause);
}
}
private void ReleaseBlink()
{
SteamVR_Fade.Start(Color.clear, fadeInTime);
fadeInTime = 0f;
}
private void InitDestinationMarkerListeners()
{
var controllerManager = GameObject.FindObjectOfType<SteamVR_ControllerManager>();
InitDestinationSetListener(controllerManager.left);
InitDestinationSetListener(controllerManager.right);
foreach (var destinationMarker in GameObject.FindObjectsOfType<VRTK_DestinationMarker>())
{
if (destinationMarker.gameObject != controllerManager.left && destinationMarker.gameObject != controllerManager.right)
{
InitDestinationSetListener(destinationMarker.gameObject);
}
}
}
private void InitHeadsetCollisionListener()
{
var headset = GameObject.FindObjectOfType<VRTK_HeadsetCollisionFade>();
if (headset)
{
headset.HeadsetCollisionDetect += new HeadsetCollisionEventHandler(DisableTeleport);
headset.HeadsetCollisionEnded += new HeadsetCollisionEventHandler(EnableTeleport);
}
}
private void DisableTeleport(object sender, HeadsetCollisionEventArgs e)
{
enableTeleport = false;
}
private void EnableTeleport(object sender, HeadsetCollisionEventArgs e)
{
enableTeleport = true;
}
}
And here is the code for the pointer:
namespace VRTK{
using UnityEngine;
using System.Collections;
public class VRTK_BezierPointer : VRTK_WorldPointer
{
public float pointerLength = 10f;
public int pointerDensity = 10;
public bool showPointerCursor = true;
public float pointerCursorRadius = 0.5f;
public float beamCurveOffset = 1f;
public GameObject customPointerTracer;
public GameObject customPointerCursor;
public LayerMask layersToIgnore = Physics.IgnoreRaycastLayer;
private GameObject projectedBeamContainer;
private GameObject projectedBeamForward;
private GameObject projectedBeamJoint;
private GameObject projectedBeamDown;
private GameObject pointerCursor;
private GameObject curvedBeamContainer;
private CurveGenerator curvedBeam;
// Use this for initialization
protected override void Start()
{
base.Start();
InitProjectedBeams();
InitPointer();
TogglePointer(false);
}
protected override void Update()
{
base.Update();
if (projectedBeamForward.gameObject.activeSelf)
{
ProjectForwardBeam();
ProjectDownBeam();
DisplayCurvedBeam();
SetPointerCursor();
}
}
protected override void InitPointer()
{
pointerCursor = (customPointerCursor ? Instantiate(customPointerCursor) : CreateCursor());
pointerCursor.name = string.Format("[{0}]WorldPointer_BezierPointer_PointerCursor", this.gameObject.name);
Utilities.SetPlayerObject(pointerCursor, VRTK_PlayerObject.ObjectTypes.Pointer);
pointerCursor.layer = LayerMask.NameToLayer("Ignore Raycast");
pointerCursor.SetActive(false);
curvedBeamContainer = new GameObject(string.Format("[{0}]WorldPointer_BezierPointer_CurvedBeamContainer", this.gameObject.name));
Utilities.SetPlayerObject(curvedBeamContainer, VRTK_PlayerObject.ObjectTypes.Pointer);
curvedBeamContainer.SetActive(false);
curvedBeam = curvedBeamContainer.gameObject.AddComponent<CurveGenerator>();
curvedBeam.transform.parent = null;
curvedBeam.Create(pointerDensity, pointerCursorRadius, customPointerTracer);
base.InitPointer();
}
protected override void SetPointerMaterial()
{
if (pointerCursor.GetComponent<Renderer>())
{
pointerCursor.GetComponent<Renderer>().material = pointerMaterial;
}
foreach (Renderer mr in pointerCursor.GetComponentsInChildren<Renderer>())
{
mr.material = pointerMaterial;
}
base.SetPointerMaterial();
}
protected override void TogglePointer(bool state)
{
state = (pointerVisibility == pointerVisibilityStates.Always_On ? true : state);
projectedBeamForward.gameObject.SetActive(state);
projectedBeamJoint.gameObject.SetActive(state);
projectedBeamDown.SetActive(state);
}
protected override void DisablePointerBeam(object sender, ControllerInteractionEventArgs e)
{
base.DisablePointerBeam(sender, e);
TogglePointerCursor(false);
curvedBeam.TogglePoints(false);
}
protected override void OnDestroy()
{
base.OnDestroy();
if (projectedBeamDown != null)
{
Destroy(projectedBeamDown);
}
if (pointerCursor != null)
{
Destroy(pointerCursor);
}
if (curvedBeam != null)
{
Destroy(curvedBeam);
}
if (projectedBeamContainer != null)
{
Destroy(projectedBeamContainer);
}
if (curvedBeamContainer != null)
{
Destroy(curvedBeamContainer);
}
}
private GameObject CreateCursor()
{
var cursorYOffset = 0.02f;
var cursor = GameObject.CreatePrimitive(PrimitiveType.Cylinder);
cursor.GetComponent<MeshRenderer>().shadowCastingMode = UnityEngine.Rendering.ShadowCastingMode.Off;
cursor.GetComponent<MeshRenderer>().receiveShadows = false;
cursor.transform.localScale = new Vector3(pointerCursorRadius, cursorYOffset, pointerCursorRadius);
Destroy(cursor.GetComponent<CapsuleCollider>());
return cursor;
}
private void TogglePointerCursor(bool state)
{
var pointerCursorState = (showPointerCursor && state ? showPointerCursor : false);
var playAreaCursorState = (showPlayAreaCursor && state ? showPlayAreaCursor : false);
pointerCursor.gameObject.SetActive(pointerCursorState);
base.TogglePointer(playAreaCursorState);
}
private void InitProjectedBeams()
{
projectedBeamContainer = new GameObject(string.Format("[{0}]WorldPointer_BezierPointer_ProjectedBeamContainer", this.gameObject.name));
Utilities.SetPlayerObject(projectedBeamContainer, VRTK_PlayerObject.ObjectTypes.Pointer);
projectedBeamContainer.transform.parent = this.transform;
projectedBeamContainer.transform.localPosition = Vector3.zero;
projectedBeamForward = new GameObject(string.Format("[{0}]WorldPointer_BezierPointer_ProjectedBeamForward", this.gameObject.name));
Utilities.SetPlayerObject(projectedBeamForward, VRTK_PlayerObject.ObjectTypes.Pointer);
projectedBeamForward.transform.parent = projectedBeamContainer.transform;
projectedBeamJoint = new GameObject(string.Format("[{0}]WorldPointer_BezierPointer_ProjectedBeamJoint", this.gameObject.name));
Utilities.SetPlayerObject(projectedBeamJoint, VRTK_PlayerObject.ObjectTypes.Pointer);
projectedBeamJoint.transform.parent = projectedBeamContainer.transform;
projectedBeamJoint.transform.localScale = new Vector3(0.01f, 0.01f, 0.01f);
projectedBeamDown = new GameObject(string.Format("[{0}]WorldPointer_BezierPointer_ProjectedBeamDown", this.gameObject.name));
Utilities.SetPlayerObject(projectedBeamDown, VRTK_PlayerObject.ObjectTypes.Pointer);
}
private float GetForwardBeamLength()
{
var actualLength = pointerLength;
Ray pointerRaycast = new Ray(transform.position, transform.forward);
RaycastHit collidedWith;
var hasRayHit = Physics.Raycast(pointerRaycast, out collidedWith, pointerLength, ~layersToIgnore);
//reset if beam not hitting or hitting new target
if (!hasRayHit || (pointerContactTarget && pointerContactTarget != collidedWith.transform))
{
pointerContactDistance = 0f;
}
//check if beam has hit a new target
if (hasRayHit)
{
pointerContactDistance = collidedWith.distance;
}
//adjust beam length if something is blocking it
if (hasRayHit && pointerContactDistance < pointerLength)
{
actualLength = pointerContactDistance;
}
return actualLength;
}
private void ProjectForwardBeam()
{
var setThicknes = 0.01f;
var setLength = GetForwardBeamLength();
//if the additional decimal isn't added then the beam position glitches
var beamPosition = setLength / (2 + 0.00001f);
projectedBeamForward.transform.localScale = new Vector3(setThicknes, setThicknes, setLength);
projectedBeamForward.transform.localPosition = new Vector3(0f, 0f, beamPosition);
projectedBeamJoint.transform.localPosition = new Vector3(0f, 0f, setLength - (projectedBeamJoint.transform.localScale.z / 2));
projectedBeamContainer.transform.localRotation = Quaternion.identity;
}
private void ProjectDownBeam()
{
projectedBeamDown.transform.position = new Vector3(projectedBeamJoint.transform.position.x, projectedBeamJoint.transform.position.y, projectedBeamJoint.transform.position.z);
Ray projectedBeamDownRaycast = new Ray(projectedBeamDown.transform.position, Vector3.down);
RaycastHit collidedWith;
var downRayHit = Physics.Raycast(projectedBeamDownRaycast, out collidedWith, float.PositiveInfinity, ~layersToIgnore);
if (!downRayHit || (pointerContactTarget && pointerContactTarget != collidedWith.transform))
{
if (pointerContactTarget != null)
{
base.PointerOut();
}
pointerContactTarget = null;
destinationPosition = Vector3.zero;
}
if (downRayHit)
{
projectedBeamDown.transform.position = new Vector3(projectedBeamJoint.transform.position.x, projectedBeamJoint.transform.position.y - collidedWith.distance, projectedBeamJoint.transform.position.z);
projectedBeamDown.transform.localScale = new Vector3(0.1f, 0.1f, 0.1f);
pointerContactTarget = collidedWith.transform;
destinationPosition = projectedBeamDown.transform.position;
base.PointerIn();
}
}
private void SetPointerCursor()
{
if (pointerContactTarget != null)
{
TogglePointerCursor(true);
pointerCursor.transform.position = projectedBeamDown.transform.position;
base.SetPlayAreaCursorTransform(pointerCursor.transform.position);
UpdatePointerMaterial(pointerHitColor);
}
else
{
TogglePointerCursor(false);
UpdatePointerMaterial(pointerMissColor);
}
}
private void DisplayCurvedBeam()
{
Vector3[] beamPoints = new Vector3[]
{
this.transform.position,
projectedBeamJoint.transform.position + new Vector3(0f, beamCurveOffset, 0f),
projectedBeamDown.transform.position,
projectedBeamDown.transform.position,
};
curvedBeam.SetPoints(beamPoints, pointerMaterial);
if (pointerVisibility != pointerVisibilityStates.Always_Off)
{
curvedBeam.TogglePoints(true);
}
}
}
}
How are you trying to pick the object. Do you use some sort of ray cast or just pick up on collision ? Nevertheless whatever you want to do triggering the teleportation when the object is picked up should be as simple as calling the teleport script from whatever script you using to pick up the object.
for eg:
void OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag ("Pick Up"))
{
<<call your teleport script here>>
}
}
Unity has really nice documentation for scripting and you can find a lot of tutorials that explain the basics, even if you go through one of them you should be able to do mostly anything you want with relative ease.

Unity3D: How to show GUI.box if condition is true?

I'm making a game in Unity3D with C#. I am using GUI.box to show a healthbar for the mobs, but I only want to show the GUI.box if there is a target.
This is my code at the moment.
public GameObject target;
public bool existsTarget;
// Use this for initialization
void Start()
{
PlayerAttack pa = (PlayerAttack)GetComponent("PlayerAttack");
target = pa.target;
existsTarget = false;
}
// Update is called once per frame
void Update()
{
if(target != null)
existsTarget = true;
else
existsTarget = false;
}
void OnGUI()
{
if(existsTarget)
GUI.Box(new Rect(500, 10, healthBarLength, 20), curHealth + "/" + maxHealth);
else {
GUI.Box(new Rect(Screen.width, 10, healthBarLength, 20), curHealth + "/" + maxHealth);
}
Unfortunately this doesn't show any healthbars at all. Any ideas as to why?
Posting the scripts here after popular demand.
public class Targetting : MonoBehaviour {
public List<Transform> targets;
public List<Transform> items;
public GameObject TheSelectedTarget {get; set;}
private Transform selectedTarget;
private Transform selectedItem;
private Transform myTransform;
// Use this for initialization
void Start () {
targets = new List<Transform>();
items = new List<Transform>();
selectedTarget = null;
selectedItem = null;
myTransform = transform;
TheSelectedTarget = null;
addAllEnemies();
addAllItems();
}
//adds all targets to a list
private void addAllEnemies() {
GameObject[] go = GameObject.FindGameObjectsWithTag("Enemy");
foreach(GameObject enemy in go){
addTarget(enemy.transform);
}
}
//adds a target
private void addTarget(Transform enemy) {
targets.Add(enemy);
}
//sorts target by distance
private void sortTargets() {
targets.Sort(delegate(Transform t1, Transform t2) {
return Vector3.Distance(t1.position, myTransform.position).CompareTo(Vector3.Distance(t2.position, myTransform.position));
});
}
//targets an enemy
private void targetEnemy() {
addAllEnemies();
if(selectedTarget == null) {
sortTargets();
selectedTarget = targets[0];
} else {
int index = targets.IndexOf(selectedTarget);
if(index < targets.Count -1) {
index++;
} else {
index = 0;
}
deselectTarget();
selectedTarget = targets[index];
}
selectTarget();
targets.Clear();
}
//selects a specific target, and colors it red
public void selectTarget() {
selectedTarget.renderer.material.color = Color.red;
PlayerAttack pa = (PlayerAttack)GetComponent("PlayerAttack");
pa.target = selectedTarget.gameObject;
TheSelectedTarget = pa.target;
}
//deselects the current selected target, and colors i grey
private void deselectTarget() {
selectedTarget.renderer.material.color = Color.gray;
selectedTarget = null;
}
//adds all items to a list
void addAllItems() {
GameObject[] go = GameObject.FindGameObjectsWithTag("Book");
foreach(GameObject book in go){
addItem(book.transform);
}
}
'
.... And then the script continues but without any relevance to this...
' public class EnemyHealth : MonoBehaviour
{
public string enemyName;
public int maxHealth = 100;
public int curHealth = 100;
public float healthBarLength;
public GameObject target;
public bool existsTarget;
public AudioSource dying;
// Use this for initialization
void Start()
{
//enemyName = this.enemyName;
healthBarLength = Screen.width / 3;
existsTarget = false;
}
// Update is called once per frame
void Update()
{
adjustCurHealth(0);
Targetting ta = (Targetting)GetComponent("Targetting");
target = ta.TheSelectedTarget;
Debug.Log (target);
if(target != null)
existsTarget = true;
else {
existsTarget = false;
}
}
void OnGUI()
{
if(existsTarget)
GUI.Box(new Rect(500, 10, healthBarLength, 20), curHealth + "/" + maxHealth);
else {
GUI.Box(new Rect(Screen.width, 10, healthBarLength, 20), curHealth + "/" + maxHealth);
}
}
public void adjustCurHealth(int adj)
{
curHealth += adj;
if (curHealth < 0)
curHealth = 0;
if (curHealth > 100)
curHealth = 100;
if (maxHealth < 0)
maxHealth = 1;
if(curHealth == 0)
{
//dying.Play ();
GameObject.Destroy(gameObject);
}
healthBarLength = (Screen.width / 3) * (curHealth / (float)maxHealth);
}
}
Are you ever setting the target anywhere other than the Start() method? The code you show will only ever show a GUI.box if the PlayerAttack.Target is not null at the start. Try moving this code to the Update() method.
PlayerAttack pa = (PlayerAttack)GetComponent("PlayerAttack");
target = pa.target;
Edit:
Check to see if the target is null, that could be the issue.
target = pa.target;
Debug.Log(target);
This will either print to the log as whatever GameObject it is, or null. If it's null, then there is no target.

Categories