Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by BrainBoneGames · Jul 18, 2012 at 12:26 AM · c#variablesclassscope

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;
     }
 }
Comment
Add comment · Show 6
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image DaveA · Jul 18, 2012 at 12:40 AM 0
Share

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?

avatar image BrainBoneGames · Jul 18, 2012 at 01:02 AM 0
Share

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.

avatar image DaveA · Jul 18, 2012 at 01:13 AM 0
Share

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);

avatar image BrainBoneGames · Jul 18, 2012 at 01:28 AM 0
Share

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.

avatar image Bunny83 · Jul 18, 2012 at 01:45 AM 0
Share

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.

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image BrainBoneGames · Jul 18, 2012 at 02:12 AM 0
Share

That was the problem. It seems to be working fine now. Thanks!

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges