Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Any Idea what I could be doing wrong with this script?

Discussion in 'Scripting' started by Zhainne, Jul 10, 2014.

  1. Zhainne

    Zhainne

    Joined:
    Jul 2, 2014
    Posts:
    11
    using UnityEngine;
    using System.Collections;

    public class GameMaster : MonoBehaviour {
    public GameObject playerCharacter;
    public GameObject gameSettings;
    public Camera mainCamera;

    public int zOffset;
    public int yOffset;
    public int xRotOffset;

    private GameObject _pc;
    private PlayerCharacter _pcScript;

    // Use this for initialization
    void Start () {
    _pc = Instantiate (playerCharacter, Vector3.zero, Quaternion.identity) as GameObject;
    _pc.name = "pc";
    _pcScript = _pc.GetComponent<PlayerCharacter> ();

    zOffset = -42;
    yOffset = 21;
    xRotOffset = 17;

    mainCamera.transform.position = new Vector3(_pc.transform.position.x, _pc.transform.position.y + yOffset, _pc.transform.position.z + zOffset);
    mainCamera.transform.Rotate (xRotOffset, 0, 0);

    LoadCharacter ();
    }

    public void LoadCharacter() {
    GameObject gs = GameObject.Find ("__GameSettings");

    if (gs == null) {
    GameObject gs1 = Instantiate(gameSettings, Vector3.zero, Quaternion.identity) as GameObject;
    gs1.name = "gameSettings";
    }

    GameSettings gsScript = GameObject.Find ("__GameSettings").GetComponent<GameSettings> ();


    gsScript.LoadCharacterData();
    }
    }



    I'm getting----------
    NullReferenceException: Object reference not set to an instance of an object
    GameMaster.LoadCharacter () (at Assets/Scripts/GameMaster.cs:40)
    GameMaster.Start () (at Assets/Scripts/GameMaster.cs:29) and it points me to GameSettings gsScript = GameObject.Find ("__GameSettings").GetComponent<GameSettings> ();

    This is from the hack and slash tutorials from this video http://www.burgzergarcade.com/tutor...l-instantiating-our-character-3x#comment-4410
     
  2. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,523
    I see two issues:

    1. Please use code tags properly. :)

    2. On line 38, you're creating a new GameSettings GameObject but naming it "gameSettings". Then on line 40 you're looking for an object named "__GameSettings", which is a completely different name.

    Some suggestions:

    1. Whenever you use GameObject.Find(), always make sure the result is non-null.
    2. Set gs1.name to "__GameSettings".
    3. If you instantiate gs1, make sure to add a GameSettings component.

    Try something like this:

    Code (csharp):
    1. if (gs == null) {
    2.     // Didn't find the GameObject, so create it:
    3.     gs = Instantiate(gameSettings, Vector3.zero, Quaternion.identity) as GameObject;
    4.     gs.name = "__GameSettings";
    5. }
    6.  
    7. // Get the GameSettings component, or add it if it doesn't exist on the GameObject:
    8. GameSettings gsScript = gs.GetComponent<GameSettings>() ?? gs.AddComponent<GameSettings>();
     
  3. Zhainne

    Zhainne

    Joined:
    Jul 2, 2014
    Posts:
    11

    Ok, thx. Problem solved and yes I'll start using the code tags. Sorry.
     
  4. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,523
    No problem! I'm glad it's working! :)