• 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
0
Question by Aerogind · Aug 09, 2014 at 07:16 AM · cs0103

Spawn spots?

I am currently working on a new Parkour Simulator MMO, but the spawns are broken. This is the error I am recieving.

 Assets/Prefabs/scripts/NetworkManager.cs(42,41): error CS0103: The name `spawnSpots' does not exist in the current context

This is appearing as 3 errors, still the same one but with different lines. Here is the code.

 using UnityEngine;
 using System.Collections;
 
 public class NetworkManager : MonoBehaviour {
 
     public Camera standbyCamera;
 
     // Use this for initialization
     void Start () {
         spawnSpots = GameObject.FindObjectsOfType<SpawnSpot> ();
         Connect ();
     }
 
     void Connect() {
         PhotonNetwork.ConnectUsingSettings("1.0.0");
     }
 
     void OnGUI () {
         GUILayout.Label(PhotonNetwork.connectionStateDetailed.ToString ());
     }
 
     void OnJoinedLobby() {
         Debug.Log ("Joined");
         PhotonNetwork.JoinRandomRoom ();
     }
 
     void OnPhotonRandomJoinFailed() {
         Debug.Log ("Failed");
         PhotonNetwork.CreateRoom( null );
     }
 
     void OnJoinedRoom() {
         Debug.Log ("Joined");
         SpawnMyPlayer ();
     }
 
     void SpawnMyPlayer() {
         if (spawnSpots == null) {
         Debug.LogError ("ERROR ERROR");
         return;
     }
         SpawnSpot mySpawnSpot = spawnSpots [Random.Range (0, spawnSpots.Length)];
         PhotonNetwork.Instantiate ("PlayerController", mySpawnSpot.transform.position, mySpawnSpot.transform.rotation, 0);
         standbyCamera.enabled = false;
     }
 }



Please help?

Comment
Add comment · Show 1
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 JustFun · Aug 09, 2014 at 07:20 AM 0
Share

You must declare spawnSpots array before you can use it:

 using UnityEngine;
 using System.Collections;
  
 public class NetworkManager : MonoBehaviour {
  
     public Camera standbyCamera;
     SpawnSpot[] spawnSpots;
     ...
 

 

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by robertbu · Aug 09, 2014 at 07:17 AM

You don't declare 'spawnSpots'. At the top of the file you need:

  private SpawnSpot[] spawnSpots;
Comment
Add comment · Show 3 · 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 Aerogind · Aug 09, 2014 at 08:11 PM 0
Share

Well that fixed the errors, but now I am getting this error:

 Assets/Prefabs/scripts/NetworkManager.cs(42,17): error CS0118: `NetworkManager.spawnSpots' is a `field' but a `type' was expected

So my code now looks like this: using UnityEngine; using System.Collections; public class NetworkManager : MonoBehaviour { private SpawnSpot[] spawnSpots; public Camera standbyCamera; // Use this for initialization void Start () { spawnSpots = GameObject.FindObjectsOfType<spawnSpots>(); Connect (); } void Connect() { PhotonNetwork.ConnectUsingSettings("1.0.0"); } void OnGUI () { GUILayout.Label(PhotonNetwork.connectionStateDetailed.ToString ()); } void OnJoinedLobby() { Debug.Log ("Joined"); PhotonNetwork.JoinRandomRoom(); } void OnPhotonRandomJoinFailed() { Debug.Log ("Failed"); PhotonNetwork.CreateRoom( null ); } void OnJoinedRoom() { Debug.Log ("Joined"); SpawnMyPlayer (); } void SpawnMyPlayer() { if (spawnSpots == null) { Debug.LogError ("ERROR ERROR"); return; } spawnSpots mySpawnSpot = spawnSpots [Random.Range (0, SpawnSpot.Length)]; PhotonNetwork.Instantiate ("PlayerController", mySpawnSpot.transform.position, mySpawnSpot.transform.rotation, 0); standbyCamera.enabled = false; } }

I am so new to C#, so excuse my lack of knowledge of it. I'm just following online tutorials.

avatar image robertbu · Aug 10, 2014 at 02:31 AM 0
Share

You have to be really careful about naming things. Case matters. In your original code you use SpawnSpot with an upper case 'S' for the type of the object. So I made the name of the variable name 'spawnSpot' with a lower case 's'. This use is by convention and is pretty typical:

  Rect rect = new Rect(0,0,10,10);
  Color color = Color.red;

Now I look at your edited code. You changed the name of the type to 'spawnSpots' with a lower case 's'. Now the name of the variable and the name of the class are the same. So line 39 becomes very confusing. Then you use SpawnSpot with an upper case 'S' in the Random.Range() call. 'SpawnSpot' was the original name of the class.

Carefully go back through your code. I'd do:

  • SpawnSpot the type of a single spawn instance (as defined in another file)

  • spawnSpots - The array of the type SpawnSpot

  • mySpawnSpot - a variable of type SpawnSpot

Note making the class 'SpawnSpot' means the class name in the file where SpawnSpot is defined must also have this name.

avatar image Aerogind · Aug 13, 2014 at 12:40 AM 0
Share

Thanks, it finally was fixed! :)

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

How does one write an AI Script for a 3rd person shooter? 0 Answers

How to access scripts other than by name. 1 Answer

How to play loop an audio clip on button hold down? 3 Answers

Having script accessing issues 2 Answers

Call inspector editor script function from another editor script 2 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges