I purchased an asset in the Unity Asset store to help with the lvl selection/lobby of my multiplayer game and everything works great except now I want to try and understand the code to be able to extract selected maps images and use them in a menu and I’m a bit lost:
This bit of code is fairly understandable to me. The integer “idmap” is the same as the “ActuMap” in the class “ChangeMaps” (Where the maps are selected)
But The last line: “ChangeMaps.inst.ApplyIconMap(idmap);” applies the map with the right id to a public Image IconMap; in the class “ChangeMaps” and I don’t understand how I can get that same image and use it on another game objects source image. I’ve tried referencing the ChangeMaps script and using the same line of code to apply the map Icon to the new game object like so: (I used this code on the new game object):
Management m; //(This class is where the code above comes from)
public Button BtnSelectM;
void Start () {
BtnSelectM.GetComponent<Button>().interactable = true;
imageMap1 = GetComponent<Image>();
}
void Update () {
OnSelectLvlClick();
}
public void SelectLvl()
{
m.idmap = ChangeMaps.inst.ActuMap;
ChangeMaps.inst.ApplyIconMap(m.idmap);}
}
public void OnSelectLvlClick()
{
BtnSelectM.onClick.AddListener(SelectLvl);
}
}
}
But there’s an Error message saying the object is not referenced… Can anyone help me please?! I’m completely stuck on this
You are not assigning anything to the variable m, so it stays null. Later, when you try resolving m.idmap m is not assigned and it throws the error, I guess.
I am not sure, if you need the Management class at all, but I don’t know the asset that you are using. However, it seems there is a singleton class ChangeMaps and it might just offer some method to get the image of the current level? Something like ChangeMaps.inst.ActuMapIcon or something?
and in the Management class it’s called upon as the above code line 2: " int idmap = ChangeMaps.inst.ActuMap;" (this is the first bit of code I posted on this thread)
with your input I’ve assigned the variable m to be the Management script by referencing the game object it is attached to in the Start function now, but it’s still the same error for some reason. Can you see a clue in the code?
EDIT: the Error reads:
NullReferenceException: Object reference not set to an instance of an object SelectTenLvl.SelectLvl () (at Assets/Lobby System Photon/Scripts/Photon/SelectTenLvl.cs:56) UnityEngine.Events.InvokableCall.Invoke () (at C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:165) UnityEngine.Events.UnityEvent.Invoke () (at C:/buildslave/unity/build/Runtime/Export/UnityEvent_0.cs:58) UnityEngine.UI.Button.Press () (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/Button.cs:36) UnityEngine.UI.Button.OnPointerClick (UnityEngine.EventSystems.PointerEventData eventData) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/Button.cs:45) UnityEngine.EventSystems.ExecuteEvents.Execute (UnityEngine.EventSystems.IPointerClickHandler handler, UnityEngine.EventSystems.BaseEventData eventData) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/ExecuteEvents.cs:50) UnityEngine.EventSystems.ExecuteEvents.Execute[T] (UnityEngine.GameObject target, UnityEngine.EventSystems.BaseEventData eventData, UnityEngine.EventSystems.ExecuteEvents+EventFunction`1[T1] functor) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/ExecuteEvents.cs:261) UnityEngine.EventSystems.EventSystem:Update()
Would be helpful to see the error message. The code of the asset you are using is quite strange and I am no psychic. But it seems the image you are looking for is ChangeMaps.inst.MapList[ChangeMaps.inst.ActuMap]. I don’t know if MapList is public, though.
Don’t you have to provide those images to the asset in the first place, anyway?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class ChangeMaps : Photon.MonoBehaviour {
public static ChangeMaps inst;
public List<MPMaps> MapList = new List<MPMaps>();
public int ActuMap;
public Image IconMap;
public Text nameMap;
int totalCountMaps;
void Awake () {
inst = this;
}
void Start()
{
totalCountMaps = MapList.Count;
}
public void ApplyIconMap (int idmap) {
ActuMap = idmap;
IconMap.sprite = MapList[ActuMap].ImgMap;
nameMap.text = MapList[ActuMap].Name;
}
public void ArrowLchangeMap()
{
if (PhotonNetwork.isMasterClient)
{
ActuMap--;
if (ActuMap < 0)
{
ActuMap = totalCountMaps-1;
}
photonView.RPC("ChangeMapsToAll", PhotonTargets.All, ActuMap);
}
}
public void ArrowRchangeMap()
{
if (PhotonNetwork.isMasterClient)
{
ActuMap++;
if (ActuMap >= totalCountMaps)
{
ActuMap = 0;
}
photonView.RPC("ChangeMapsToAll", PhotonTargets.All, ActuMap);
}
}
[PunRPC]
public void ChangeMapsToAll(int idmap)
{
ActuMap = idmap;
IconMap.sprite = MapList[ActuMap].ImgMap;
nameMap.text = MapList[ActuMap].Name;
}
}
[System.Serializable]
public class MPMaps
{
public int id;
public string Name;
public Sprite ImgMap;
public string NameScene;
}
I posted the full error in the edit of my last reply. The images are assigned in the inspector yes and they are selected via arrows, you dont have to press anything you simply land on the map you want to play and press start match.
Sorry I should’ve formulated my question better, I see now why it’s confusing.
In my script which is the second bit of code I posted, in line 6 : " imageMap1 = GetComponent();"
imageMap1 is where I want the map Icon to show. so its
public Image imageMap1;
But I’m not using it anywhere. Sorry I shouldve mentioned that. So basically I’m looking for something like: