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 Salman12340 · Feb 19, 2016 at 12:30 AM · errorvector3error message

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

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 Nerevar · Feb 19, 2016 at 08:51 AM 0
Share

Something might be wrong with Waypoints$$anonymous$$over. (line 82) what is this object for?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

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

Visual Studio Assembly C sharp (unavailable) 0 Answers

Error when import UnityPurchasing "CS1704: An assembly with the same name `Editor' has already been imported" 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