- Home /
Class Scope Variable
This code is part of the package Tile Based Map and Nav. What I'm trying to get out of it is, get the node that was clicked on and send an event to playmaker to activate some GUI buttons. When you click one of the buttons it selects the prefab and sends a message from playmaker to invoke the spawn function at the node location. The problem I'm running in to is that when I put the Unit.SpawnUnit line outside of the Update method, it can't find the variable "node".
I've been told that this is because the variable isn't being saved and that I should create a Class Scope Variable to save the node information in. Well, I looked up how to create one and I'm not really understanding it. I need some insight.
using UnityEngine;
using System.Collections;
using HutongGames.PlayMaker;
public class GameControllerIOW : MonoBehaviour
{
// This is a simple sample of how to spawn a unit on a tile that was clicked
public CameraMove camMover; // used to move camera around (like make it follow a transform)
public Camera rayCam;
public GameObject unitFab; // unit prefab
public MapNav map; // the mapnav
public LayerMask tilesLayer; // layer the tiles are on
public PlayMakerFSM behavior; // playmaker behavior
public int nodeId;
public Vector3 nodeLoc;
public TileNode node = null;
IEnumerator Start()
{
// wait for a frame for everything else to start and then enable the colliders for the TielNodes
yield return null;
// now enable the colliders of the TileNodes.
// they are disabled by default, but for this sample to work I need the player to be able to click on any tile.
// for your game you will have to decide when the best time would be to this or even which tiles would be
// best to enable. For example, you might only want to spawn new units around some building, so only
// enable the the tiles around the building so that the player cannot click on other tiles and disable
// the tiles whne yo uare done with them
foreach (TileNode n in map.nodes)
{
n.collider.enabled = true;
}
}
void Update()
{
// don;t do anything else if there was not a mouse click
if (!Input.GetMouseButtonUp(0)) return;
// cast a ray to check what the player "clicked" on. Only want to know
// about TILE clicks, so pass mask to check against layer for tiles only
Ray ray = rayCam.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit, 500f, tilesLayer))
{
// a tile gameobject was clicked on
// get the TileNode
TileNode node = hit.collider.GetComponent<TileNode>();
if (node == null) return; // sanity check
nodeId = node.idx;
//nodeLoc = node.transform.position;
// dont spawn here if there is alrelady a unit on this tile
//if (node.units.Count > 0) return;
// send events to Playmaker
behavior.Fsm.Event("TileNodeActive");
//Unit.SpawnUnit(unitFab, map, node);
}
}
void Spawn ()
{
if (node == null) return; // sanity check
// dont spawn here if there is alrelady a unit on this tile
if (node.units.Count > 0) return;
Unit.SpawnUnit(unitFab, map, node);
behavior.Fsm.Event("Unit Spawned");
nodeLoc = node.transform.position;
}
}
You really need to format this code.
What do you mean by "put the Unit.SpawnUnit line outside of the Update method" Where do you put it?
I mean that when I create void Spawn () and put the line Unit.SpawnUnit(unitFab, map, node) that it can't find the "node" variable outside of the context.
if this line has problems, it can't find node:
if (node == null) return; // sanity check
if this line has problems, maybe it can'f find Unit (I don't see where that is declared):
Unit.SpawnUnit(unitFab, map, node);
I think Unit is declared in another script. The code that I pasted above is from a controller script. You'll have to forgive me b/c I only have a really basic knowledge of scripting.
I'm mostly concerned about this line TileNode node = hit.collider.GetComponent();
This is where I need to catch the node variable to be able to use it outside of the Update method. I was told to make a Class Scope Variable but I wouldn't know where to put it or the syntax. That's mainly where I need help.
Just as a general hint: comments like in this line:
public GameObject unitFab; // unit prefab
Are really, really bad style. They are absolutely pointless and produce even more problems than they are helpful.
Only use comments if you need to give further information that is needed to understand why you've done this ot that. Code should be self explanatory by using proper variable- / method- / class- names.
Answer by Bunny83 · Jul 18, 2012 at 01:35 AM
Your mistake is that now you have a member variable "node" (class variable) and a local variable "node". You should just use the member variable. So change this line:
TileNode node = hit.collider.GetComponent<TileNode>();
into this:
node = hit.collider.GetComponent<TileNode>();
This will use the member variable instead of creating a local variable.
That was the problem. It seems to be working fine now. Thanks!
Your answer
Follow this Question
Related Questions
why does my object reference go out of scope? 1 Answer
Can I choose what to pass into .getcomponent<{VARIABLE}>(); 1 Answer
Does Garbage Collector Collect variables Made Inside Scope? 1 Answer
I want to assign multiple variables without having to type the class on the side every time. 1 Answer
Multiple Cars not working 1 Answer