Hi,
So I'm trying to do this. When the player is in the interactive boundary, I want an icon to show over the hotspot using the SetIconVisibility function.
This is the code.
`using UnityEngine;
using System.Collections.Generic;
namespace AC
{
/**
* Used to limit a Hotspot's interactivity to Players that are within a given volume.
* Attach this to a Trigger collider, and assign in a Hotspot's Inspector. When assigned, the Hotspot will only be interactable when the Player is within the collider's boundary.
*/
[AddComponentMenu("Adventure Creator/Hotspots/Interactive boundary")]
[HelpURL("https://www.adventurecreator.org/scripting-guide/class_a_c_1_1_interactive_boundary.html")]
public class InteractiveBoundary : MonoBehaviour
{
#region Variables
protected bool forcePresence;
protected List<GameObject> playersPresent = new List<GameObject>();
private Hotspot currentSpot;
#endregion
#region UnityStandards
private void OnEnable()
{
currentSpot = GetComponent<Hotspot>();
}
protected void OnTriggerEnter (Collider other)
{
if (KickStarter.player && other.gameObject == KickStarter.player.gameObject && !playersPresent.Contains (other.gameObject))
{
currentSpot.SetIconVisibility(true, 5f);
playersPresent.Add (other.gameObject);
}
}
protected void OnTriggerExit (Collider other)
{
if (KickStarter.player && other.gameObject == KickStarter.player.gameObject && playersPresent.Contains (other.gameObject))
{
currentSpot.SetIconVisibility(false, 5f);
playersPresent.Remove (other.gameObject);
}
}
protected void OnTriggerEnter2D (Collider2D other)
{
if (KickStarter.player && other.gameObject == KickStarter.player.gameObject && !playersPresent.Contains (other.gameObject))
{
playersPresent.Add (other.gameObject);
}
}
protected void OnTriggerExit2D (Collider2D other)
{
if (KickStarter.player && other.gameObject == KickStarter.player.gameObject && playersPresent.Contains (other.gameObject))
{
playersPresent.Remove (other.gameObject);
}
}
#endregion
#region GetSet
/** True if the active Player is within the Collider boundary */
public bool PlayerIsPresent
{
get
{
if (forcePresence)
{
return true;
}
return (KickStarter.player && playersPresent.Contains (KickStarter.player.gameObject));
}
}
/** If True, the Player will always be considered as present within the Collider boundary, even when not physically so */
public bool ForcePresence
{
set
{
forcePresence = value;
}
}
#endregion
}
}`
However when I enter the boundary, it doesnt work. I get;
"NullReferenceException: Object reference not set to an instance of an object
AC.InteractiveBoundary.OnTriggerEnter (UnityEngine.Collider other) (at Assets/AdventureCreator/Scripts/Object/InteractiveBoundary.cs:50)
"
It looks like you're new here. If you want to get involved, click one of these buttons!
Comments
What is on line 50, and is it attached to a Hotspot?
This is the interactive boundary script attached to the interactive boundary object. The interactive boundary object is a child of the hotspot.
You're setting currentSpot to the Hotspot attached to the same object:
Remove this, and make currentSpot public so that you can assign it to the correct Hotspot in the Inspector.
Alright its working thanks. How do i change the icon that shows?
It's either a fixed texture for all Hotspots, or each Hotspot's first-defined Use icon, depending on the Settings Manager's Hotspot icon type field.