1

I have this error on line 38 of the script below.I know that there were some other questions of the same type.But none was helpful to me.This error have stopped my project.And I want to get rid of it quickly.So, if someone could help me i would be greatly thakful.Here is my code

using System.Collections.Generic;
using UnityEngine;


public class LootChest : MonoBehaviour
{
public int MaxItems;
int ItemCount;
List<Item> Items = new List<Item>();
public GameManager GameManager;
bool Selected;
public float Distance;
public Color HoverColour;
public Color ClickColour;
Color DefaultColor;
// Use this for initialization
void Start()
{
    ItemCount = Random.Range(0, MaxItems);
    for (int i = 0; i < ItemCount; i++)
    {
        int r = Random.Range(0, GameManager.Instance.AllItems.Count - 1);
        Items.Add(GameManager.Instance.AllItems[r]);
    }

    DefaultColor = GetComponent<Renderer>().material.color;
}

// Update is called once per frame
void Update()
{


}

void OnMouseOver()
{
    if (Vector3.Distance(transform.position, GameManager.Instance.CurrentCharacter.Instance.transform.position) > Distance);
    GetComponent<Renderer>().material.color = HoverColour;
}

void OnMouseExit()
{
    if (Vector3.Distance(transform.position, GameManager.Instance.CurrentCharacter.Instance.transform.position) < Distance) ;
    GetComponent<Renderer>().material.color = DefaultColor;
}
void OnMouseDown()
{

}
void OnMouseUp()
{

}
}

The error is on line

   if (Vector3.Distance(transform.position, GameManager.Instance.CurrentCharacter.Instance.transform.position) < Distance) ;

This error dont show up in the script editor(Visual studio 2017).And I think this is a unity based error not script based. This is the image of unity with the cube object .

Here is the GameManager script

using UnityEngine;
using System.Collections.Generic;

  public class GameManager : MonoBehaviour
 {
  public List<Character> Characters = new List<Character>();
  public List <Item> AllItems = new List<Item> ();
  bool ShowCharWheel;
  public int SelectedCharacter;
  int LastCharacter;
  public static GameManager Instance;
  public bool CanShowSwitch = true;
  public Character CurrentCharacter;
  public LootChest SelectedChest;

void Awake()
{
    foreach (Character c in Characters)
    {
        c.Instance = Instantiate(c.PlayerPrefab, c.HomeSpawn.position,      c.HomeSpawn.rotation) as GameObject;
        c.Instance.GetComponent <PlayerController> ().LocalCharacter = c;
    }
    ChangeCharacterStart(Characters[PlayerPrefs.GetInt("SelectedChar")]);
}
// Use this for initialization
void Start()
{

}
void ChangeCharacterStart(Character c)
{

    LastCharacter = SelectedCharacter;
    SelectedCharacter = Characters.IndexOf(c);
    CurrentCharacter = c;
    Characters [LastCharacter].Instance.GetComponent<PlayerController> ().CanPlay = false;
    Characters[SelectedCharacter].Instance.GetComponent<PlayerController>().CanPlay = true;
    Camera.main.GetComponent<asd>().target = Characters[SelectedCharacter].Instance.transform;
    PlayerPrefs.SetInt("SelectedChar", SelectedCharacter);
}
// Update is called once per frame
void Update()
{
    if (CanShowSwitch) {
        if (Input.GetKey (KeyCode.C)) {
            ShowCharWheel = true;

        } else if (Input.GetKey (KeyCode.V)) {
            ShowCharWheel = false;

        }

    }
}

void ChangeCharacter(Character c)
{
    c.Instance.GetComponent<AI> ().DoneHome = false;
    if (Vector3.Distance (Characters [SelectedCharacter].Instance.transform.position, c.Instance.transform.position) > 10) {
        sequencemanager.Instance.StartCoroutine ("DoCharSwitch", c);
        CanShowSwitch = false;
        LastCharacter = SelectedCharacter;
        SelectedCharacter = Characters.IndexOf (c);
        CurrentCharacter = c;
        Characters [LastCharacter].Instance.GetComponent<PlayerController> ().CanPlay = false;
        Characters [SelectedCharacter].Instance.GetComponent<PlayerController> ().CanPlay = true;
        PlayerPrefs.SetInt ("SelectedChar", SelectedCharacter);

    } else {
        LastCharacter = SelectedCharacter;
        SelectedCharacter = Characters.IndexOf(c);
        CurrentCharacter = c;
        Characters [LastCharacter].Instance.GetComponent<PlayerController> ().CanPlay = false;
        Characters [SelectedCharacter].Instance.GetComponent<PlayerController> ().CanPlay = true;
        PlayerPrefs.SetInt ("SelectedChar", SelectedCharacter);
        Camera.main.GetComponent<asd> ().target = Characters [SelectedCharacter].Instance.transform;

    }


}


void OnGUI()
{
    if (ShowCharWheel)
    {
        GUILayout.BeginArea(new Rect(Screen.width - 64, Screen.height - 256, 64, 208), GUIContent.none, "box");
        foreach (Character c in Characters)
        {
            if (GUILayout.Button(c.Icon, GUILayout.Width(64), GUILayout.Height(64)))
            {
                ChangeCharacterStart(c);
            }
        }
        GUILayout.EndArea();
    }
    }
    }

  [System.Serializable]
  public class Character
{
  public string Name;
  public Texture2D Icon;
  public GameObject PlayerPrefab;
  public GameObject Instance;
  public Transform HomeSpawn;
   }

  [System.Serializable]
  public class Item
  {
  public string Name;
  public Texture2D Icon;
  public ItemInstance InstancePrefab;
 }
S.Saad
  • 41
  • 1
  • 9
  • 1
    Your script is expecting a `GameManager` but you didn't assign one in the editor – UnholySheep Mar 04 '17 at 13:04
  • yea the problem IMO seems to be with GameManager – Bernard Walters Mar 04 '17 at 13:38
  • I have assigned the GameManager script to the game object Gamemanager.I will post GameManager.cs in the question. – S.Saad Mar 04 '17 at 13:39
  • @S.Saad GameManager gameManager = new GameManager(); – Bernard Walters Mar 04 '17 at 13:53
  • 1
    I don't think the gamemanager is the problem as he uses that in the Start() function already and that doesn't give any errors. The problem is probably that the GameManager.Instance.CurrentCharacter.Instance is null. To check if this is the problem, you can put some lines of code just before the error like `if(GameManager.Instance.CurrentCharacter.Instance == null) print("Found error");` – Julian Declercq Mar 04 '17 at 13:54
  • @UnholySheep Do you have some answer? – S.Saad Mar 04 '17 at 13:54
  • @BernardWalters Thanks man.Although it didn't solve my problem completely (The colour of cube still don't change)but it dont give the error now.So now I can work on my project.Thank you very much again for answering. – S.Saad Mar 04 '17 at 14:08
  • @S.Saad No problem. – Bernard Walters Mar 04 '17 at 14:17
  • @BernardWalters This was my first post on any forum about programming and I have got that much good response.So now I hope if I have a problem I can solve it quickly and easily. – S.Saad Mar 04 '17 at 14:20
  • Just debug your code. Don't guess. – zwcloud Mar 05 '17 at 04:39

0 Answers0