NullReferenceException: Object reference not set to an instance of an object
I'm new to C# and facing an issue regarding NullReferenceException: Object reference not set to an instance of an object Assets.Plugins.SmartLevelsMap.Scripts.MapCamera.SetPosition (Vector2 position) (at Assets/Plugins/SmartLevelsMap/Scripts/MapCamera.cs:79) Assets.Plugins.SmartLevelsMap.Scripts.LevelsMap.SetCameraToCharacter () (at Assets/Plugins/SmartLevelsMap/Scripts/LevelsMap.cs:82) Assets.Plugins.SmartLevelsMap.Scripts.LevelsMap.Reset () (at Assets/Plugins/SmartLevelsMap/Scripts/LevelsMap.cs:59) Assets.Plugins.SmartLevelsMap.Scripts.LevelsMap.OnEnable () (at Assets/Plugins/SmartLevelsMap/Scripts/LevelsMap.cs:46)
Here is both MapCamera.cs and LevelsMap.cs
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
namespace Assets.Plugins.SmartLevelsMap.Scripts
{
public class MapCamera : MonoBehaviour
{
private Vector2 _prevPosition;
private Transform _transform;
public Camera Camera;
public Bounds Bounds;
public void Awake()
{
_transform = transform;
}
public void OnDrawGizmos()
{
Gizmos.DrawWireCube(Bounds.center, Bounds.size);
}
public void Update()
{
#if (UNITY_ANDROID || UNITY_IPHONE) && !UNITY_EDITOR
HandleTouchInput();
#else
HandleMouseInput();
#endif
}
private void HandleTouchInput()
{
if(Input.touchCount == 1)
{
Touch touch = Input.GetTouch(0);
if(touch.phase == TouchPhase.Began)
{
_prevPosition = touch.position;
}
else if(touch.phase == TouchPhase.Moved)
{
Vector2 curPosition = touch.position;
MoveCamera(_prevPosition, curPosition);
_prevPosition = curPosition;
}
}
}
private void HandleMouseInput()
{
if (Input.GetMouseButtonDown(0))
_prevPosition = Input.mousePosition;
if (Input.GetMouseButton(0))
{
Vector2 curMousePosition = Input.mousePosition;
MoveCamera(_prevPosition, curMousePosition);
_prevPosition = curMousePosition;
}
}
private void MoveCamera(Vector2 prevPosition, Vector2 curPosition)
{
if(EventSystem.current.IsPointerOverGameObject(-1)) return;
SetPosition(
transform.localPosition +
(Camera.ScreenToWorldPoint(prevPosition) - Camera.ScreenToWorldPoint(curPosition)));
}
public void SetPosition(Vector2 position)
{
Vector2 validatedPosition = ApplyBounds(position);
_transform.position = new Vector3(validatedPosition.x, validatedPosition.y, _transform.position.z);
}
private Vector2 ApplyBounds(Vector2 position)
{
float cameraHeight = Camera.orthographicSize*2f;
float cameraWidth = (Screen.width * 1f/Screen.height)*cameraHeight;
position.x = Mathf.Max(position.x, Bounds.min.x + cameraWidth/2f);
position.y = Mathf.Max(position.y, Bounds.min.y + cameraHeight/2f);
position.x = Mathf.Min(position.x, Bounds.max.x - cameraWidth/2f);
position.y = Mathf.Min(position.y, Bounds.max.y - cameraHeight/2f);
return position;
}
}
}
LevelsMap.cs
using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
namespace Assets.Plugins.SmartLevelsMap.Scripts
{
public class LevelsMap : MonoBehaviour
{
private static LevelsMap _instance;
private static IMapProgressManager _mapProgressManager = new PlayerPrefsMapProgressManager();
public bool IsGenerated;
public MapLevel MapLevelPrefab;
public Transform CharacterPrefab;
public int Count = 10;
public WaypointsMover WaypointsMover;
public MapLevel CharacterLevel;
public TranslationType TranslationType;
public bool StarsEnabled;
public StarsType StarsType;
public bool ScrollingEnabled;
public MapCamera MapCamera;
public bool IsClickEnabled;
public bool IsConfirmationEnabled;
public void Awake()
{
_instance = this;
}
public void OnDestroy()
{
_instance = null;
}
public void OnEnable()
{
if (IsGenerated)
{
Reset();
}
}
private List<MapLevel> GetMapLevels()
{
return FindObjectsOfType<MapLevel>().OrderBy(ml => ml.Number).ToList();
}
private void Reset()
{
UpdateMapLevels();
PlaceCharacterToLastUnlockedLevel();
SetCameraToCharacter();
}
private void UpdateMapLevels()
{
foreach (MapLevel mapLevel in GetMapLevels())
{
mapLevel.UpdateState(
_mapProgressManager.LoadLevelStarsCount(mapLevel.Number),
IsLevelLocked(mapLevel.Number));
}
}
private void PlaceCharacterToLastUnlockedLevel()
{
int lastUnlockedNumber = GetMapLevels().Where(l => !l.IsLocked).Select(l => l.Number).Max();
TeleportToLevelInternal(lastUnlockedNumber, true);
}
private void SetCameraToCharacter()
{
MapCamera mapCamera = FindObjectOfType<MapCamera>();
if(mapCamera != null)
mapCamera.SetPosition(WaypointsMover.transform.position);
}
#region Events
public static event EventHandler<LevelReachedEventArgs> LevelSelected;
public static event EventHandler<LevelReachedEventArgs> LevelReached;
#endregion
#region Static API
public static void CompleteLevel(int number)
{
CompleteLevelInternal(number, 1);
}
public static void CompleteLevel(int number, int starsCount)
{
CompleteLevelInternal(number, starsCount);
}
internal static void OnLevelSelected(int number)
{
if (LevelSelected != null && !IsLevelLocked(number)) //need to fix in the map plugin
LevelSelected(_instance, new LevelReachedEventArgs(number));
if (!_instance.IsConfirmationEnabled)
GoToLevel(number);
}
public static void GoToLevel(int number)
{
switch (_instance.TranslationType)
{
case TranslationType.Teleportation:
_instance.TeleportToLevelInternal(number, false);
break;
case TranslationType.Walk:
_instance.WalkToLevelInternal(number);
break;
}
}
public static bool IsLevelLocked(int number)
{
return number > 1 && _mapProgressManager.LoadLevelStarsCount(number - 1) == 0;
}
public static void OverrideMapProgressManager(IMapProgressManager mapProgressManager)
{
_mapProgressManager = mapProgressManager;
}
public static void ClearAllProgress()
{
_instance.ClearAllProgressInternal();
}
public static bool IsStarsEnabled()
{
return _instance.StarsEnabled;
}
public static bool GetIsClickEnabled()
{
return _instance.IsClickEnabled;
}
public static bool GetIsConfirmationEnabled()
{
return _instance.IsConfirmationEnabled;
}
#endregion
private static void CompleteLevelInternal(int number, int starsCount)
{
if(IsLevelLocked(number))
{
Debug.Log(string.Format("Can't complete locked level {0}.", number));
}
else if (starsCount < 1 || starsCount > 3)
{
Debug.Log(string.Format("Can't complete level {0}. Invalid stars count {1}.", number, starsCount));
}
else
{
int curStarsCount = _mapProgressManager.LoadLevelStarsCount(number);
int maxStarsCount = Mathf.Max(curStarsCount, starsCount);
_mapProgressManager.SaveLevelStarsCount(number, maxStarsCount);
if (_instance != null)
_instance.UpdateMapLevels();
}
}
private void TeleportToLevelInternal(int number, bool isQuietly)
{
MapLevel mapLevel = GetLevel(number);
if (mapLevel.IsLocked)
{
Debug.Log(string.Format("Can't jump to locked level number {0}.", number));
}
else
{
WaypointsMover.transform.position = mapLevel.PathPivot.transform.position; //need to fix in the map plugin
CharacterLevel = mapLevel;
if (!isQuietly)
RaiseLevelReached(number);
}
}
private void WalkToLevelInternal(int number)
{
MapLevel mapLevel = GetLevel(number);
if (mapLevel.IsLocked)
{
Debug.Log(string.Format("Can't go to locked level number {0}.", number));
}
else
{
WaypointsMover.Move(CharacterLevel.PathPivot, mapLevel.PathPivot,
() =>
{
RaiseLevelReached(number);
CharacterLevel = mapLevel;
});
}
}
private void RaiseLevelReached(int number)
{
MapLevel mapLevel = GetLevel(number);
if (!string.IsNullOrEmpty(mapLevel.SceneName))
Application.LoadLevel(mapLevel.SceneName);
if (LevelReached != null)
LevelReached(this, new LevelReachedEventArgs(number));
}
private MapLevel GetLevel(int number)
{
return GetMapLevels().SingleOrDefault(ml => ml.Number == number);
}
private void ClearAllProgressInternal()
{
foreach (MapLevel mapLevel in GetMapLevels())
_mapProgressManager.ClearLevelProgress(mapLevel.Number);
Reset();
}
public void SetStarsEnabled(bool bEnabled)
{
StarsEnabled = bEnabled;
int starsCount = 0;
foreach (MapLevel mapLevel in GetMapLevels())
{
mapLevel.UpdateStars(starsCount);
starsCount = (starsCount + 1)%4;
mapLevel.StarsHoster.gameObject.SetActive(bEnabled);
mapLevel.SolidStarsHoster.gameObject.SetActive(bEnabled);
}
}
public void SetStarsType(StarsType starsType)
{
StarsType = starsType;
foreach (MapLevel mapLevel in GetMapLevels())
mapLevel.UpdateStarsType(starsType);
}
}
}
any help will be appreciated. Thanks
Something might be wrong with Waypoints$$anonymous$$over
. (line 82) what is this object for?
Answer by trandua18 · Nov 15, 2016 at 03:34 AM
_transform.position = new Vector3(validatedPosition.x, validatedPosition.y, _transform.position.z); in line 82 you replace to transform.position = new Vector3(validatedPosition.x, validatedPosition.y, transform.position.z); ,_transform.position = new Vector3(validatedPosition.x, validatedPosition.y, _transform.position.z); in line 82 replace to: transform.position = new Vector3(validatedPosition.x, validatedPosition.y, transform.position.z); it worked for me.
Your answer
Follow this Question
Related Questions
I have an error on a C# script @username 2 Answers
Nav Mesh Problem with SetDestination 1 Answer
Realidad aumentada Vuforia problema creando apk 0 Answers