Skip to main content
added 179 characters in body
Source Link

So I have a game object MapGenerator and 2 attached scripts: MapGenerator.cs and Lexicon.cs.

I need to be able to access a couple variables in Lexicon.cs from MapGenerator.cs, so I imported it with:

void Awake()
{
    GameObject gameObject = new GameObject("Lexicon");
    lexicon = gameObject.AddComponent<Lexicon>();
}

Now, this allows me to pull variables from Lexicon, and I am able to do that successfully for the most part. However, the public fields that are being set from Unity (tiles/sprites) are not being pulled in. They just show up as null.

Example scenario:

public class Lexicon : MonoBehaviour
{ 
public Tile thiccTile;
public Tile thiccTile2;
public Tile thiccTile3;
private string fact = "Yummy porchop";
private string favoritefact;
private Tile favoritetile;

 public Lexicon() { 
  favoritetile = thiccTile2; //this guy is public and set in unity under components.
  favoritefact = fact; 
 }

public Tile Favoritetile { get { return favoritetile; } set { favoritetile= value; } }
public string Favoritefact { get { return favoritefact; } set { favoritefact= value; } }

}

Start() {
UnityEngine.Debug.Log(lexicon.Favoritefact); //"Yummy porchop"
UnityEngine.Debug.Log(lexicon.Favoritetile); // null
}

My first thought was it was because I never explicitly call Lexicon(). However, since favoritefact works, im guessing the gameobject stuff is enough? Anyway, how can I fix this?

EDIT: This is where I set the values (Different variable names but you get the idea)

enter image description here

So I have a game object MapGenerator and 2 attached scripts: MapGenerator.cs and Lexicon.cs.

I need to be able to access a couple variables in Lexicon.cs from MapGenerator.cs, so I imported it with:

void Awake()
{
    GameObject gameObject = new GameObject("Lexicon");
    lexicon = gameObject.AddComponent<Lexicon>();
}

Now, this allows me to pull variables from Lexicon, and I am able to do that successfully for the most part. However, the public fields that are being set from Unity (tiles/sprites) are not being pulled in. They just show up as null.

Example scenario:

public class Lexicon : MonoBehaviour
{ 
public Tile thiccTile;
public Tile thiccTile2;
public Tile thiccTile3;
private string fact = "Yummy porchop";
private string favoritefact;
private Tile favoritetile;

 public Lexicon() { 
  favoritetile = thiccTile2; //this guy is public and set in unity under components.
  favoritefact = fact; 
 }

public Tile Favoritetile { get { return favoritetile; } set { favoritetile= value; } }
public string Favoritefact { get { return favoritefact; } set { favoritefact= value; } }

}

Start() {
UnityEngine.Debug.Log(lexicon.Favoritefact); //"Yummy porchop"
UnityEngine.Debug.Log(lexicon.Favoritetile); // null
}

My first thought was it was because I never explicitly call Lexicon(). However, since favoritefact works, im guessing the gameobject stuff is enough? Anyway, how can I fix this?

So I have a game object MapGenerator and 2 attached scripts: MapGenerator.cs and Lexicon.cs.

I need to be able to access a couple variables in Lexicon.cs from MapGenerator.cs, so I imported it with:

void Awake()
{
    GameObject gameObject = new GameObject("Lexicon");
    lexicon = gameObject.AddComponent<Lexicon>();
}

Now, this allows me to pull variables from Lexicon, and I am able to do that successfully for the most part. However, the public fields that are being set from Unity (tiles/sprites) are not being pulled in. They just show up as null.

Example scenario:

public class Lexicon : MonoBehaviour
{ 
public Tile thiccTile;
public Tile thiccTile2;
public Tile thiccTile3;
private string fact = "Yummy porchop";
private string favoritefact;
private Tile favoritetile;

 public Lexicon() { 
  favoritetile = thiccTile2; //this guy is public and set in unity under components.
  favoritefact = fact; 
 }

public Tile Favoritetile { get { return favoritetile; } set { favoritetile= value; } }
public string Favoritefact { get { return favoritefact; } set { favoritefact= value; } }

}

Start() {
UnityEngine.Debug.Log(lexicon.Favoritefact); //"Yummy porchop"
UnityEngine.Debug.Log(lexicon.Favoritetile); // null
}

My first thought was it was because I never explicitly call Lexicon(). However, since favoritefact works, im guessing the gameobject stuff is enough? Anyway, how can I fix this?

EDIT: This is where I set the values (Different variable names but you get the idea)

enter image description here

Source Link

Added tiles not loading in run time

So I have a game object MapGenerator and 2 attached scripts: MapGenerator.cs and Lexicon.cs.

I need to be able to access a couple variables in Lexicon.cs from MapGenerator.cs, so I imported it with:

void Awake()
{
    GameObject gameObject = new GameObject("Lexicon");
    lexicon = gameObject.AddComponent<Lexicon>();
}

Now, this allows me to pull variables from Lexicon, and I am able to do that successfully for the most part. However, the public fields that are being set from Unity (tiles/sprites) are not being pulled in. They just show up as null.

Example scenario:

public class Lexicon : MonoBehaviour
{ 
public Tile thiccTile;
public Tile thiccTile2;
public Tile thiccTile3;
private string fact = "Yummy porchop";
private string favoritefact;
private Tile favoritetile;

 public Lexicon() { 
  favoritetile = thiccTile2; //this guy is public and set in unity under components.
  favoritefact = fact; 
 }

public Tile Favoritetile { get { return favoritetile; } set { favoritetile= value; } }
public string Favoritefact { get { return favoritefact; } set { favoritefact= value; } }

}

Start() {
UnityEngine.Debug.Log(lexicon.Favoritefact); //"Yummy porchop"
UnityEngine.Debug.Log(lexicon.Favoritetile); // null
}

My first thought was it was because I never explicitly call Lexicon(). However, since favoritefact works, im guessing the gameobject stuff is enough? Anyway, how can I fix this?