using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class HexMap : MonoBehaviour {
// Use this for initialization
void Start () {
GenerateMap();
}
public GameObject HexPrefab;
public Material[] HexMaterials;
public readonly int NumRows = 60;
public readonly int NumColumns = 120;
public void GenerateMap()
{
for (int column = 0; column < NumColumns; column++)
{
for (int row = 0; row < NumRows; row++)
{
// Instantiate a Hex
Hex h = new Hex( column, row );
Vector3 pos = h.PositionFromCamera(
Camera.main.transform.position,
NumRows,
NumColumns
);
GameObject hexGO = (GameObject)Instantiate(
HexPrefab,
pos,
Quaternion.identity,
this.transform
);
hexGO.GetComponent<HexComponent>().Hex = h;
hexGO.GetComponent<HexComponent>().HexMap = this;
MeshRenderer mr = hexGO.GetComponentInChildren<MeshRenderer>();
mr.material = HexMaterials[ Random.Range(0, HexMaterials.Length) ];
}
}
//StaticBatchingUtility.Combine( this.gameObject );
}
}
Very late answer, but for those who stumbled on the same problem:
In documentation
(Unity - Scripting API: Camera.main)
it is stated that “Returns null if there is no such camera in the Scene. This property uses FindGameObjectsWithTag internally”.
if Camera.main is not accessible it probably means that Camera in the Scene is not tagged with ‘MainCamera’ tag. Check your tags.
when i delete “.main” unity dont like it.
// Instantiate a Hex
Hex h = new Hex( column, row );
Vector3 pos = h.PositionFromCamera(
Camera.transform.position,
NumRows,
NumColumns
);
@Harinezumi @CiaranSimpson
these are the two other scripts in my current tutorial, the issue is the same:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class HexComponent : MonoBehaviour {
public Hex Hex;
public HexMap HexMap;
public void UpdatePosition()
{
this.transform.position = Hex.PositionFromCamera(
Camera.main.transform.position,
HexMap.NumRows,
HexMap.NumColumns
);
}
}
and here the last script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// The Hex class defines the grid position, world space position, size,
/// neighbours, etc... of a Hex Tile. However, it does NOT interact with
/// Unity directly in any way.
/// </summary>
public class Hex {
public Hex(int q, int r)
{
this.Q = q;
this.R = r;
this.S = -(q + r);
}
// Q + R + S = 0
// S = -(Q + R)
public readonly int Q; // Column
public readonly int R; // Row
public readonly int S;
static readonly float WIDTH_MULTIPLIER = Mathf.Sqrt(3) / 2;
float radius = 1f;
bool allowWrapEastWest = true;
bool allowWrapNorthSouth = false;
/// <summary>
/// Returns the world-space position of this hex
/// </summary>
public Vector3 Position()
{
return new Vector3(
HexHorizontalSpacing() * (this.Q + this.R/2f),
0,
HexVerticalSpacing() * this.R
);
}
public float HexHeight()
{
return radius * 2;
}
public float HexWidth()
{
return WIDTH_MULTIPLIER * HexHeight();
}
public float HexVerticalSpacing()
{
return HexHeight() * 0.75f;
}
public float HexHorizontalSpacing()
{
return HexWidth();
}
public Vector3 PositionFromCamera( Vector3 cameraPosition, float numRows, float numColumns)
{
float mapHeight = numRows * HexVerticalSpacing();
float mapWidth = numColumns * HexHorizontalSpacing();
Vector3 position = Position();
if(allowWrapEastWest)
{
float howManyWidthsFromCamera = (position.x - cameraPosition.x) / mapWidth;
// We want howManyWidthsFromCamera to be between -0.5 to 0.5
if(howManyWidthsFromCamera > 0)
howManyWidthsFromCamera += 0.5f;
else
howManyWidthsFromCamera -= 0.5f;
int howManyWidthToFix = (int)howManyWidthsFromCamera;
position.x -= howManyWidthToFix * mapWidth;
}
if(allowWrapNorthSouth)
{
float howManyHeightsFromCamera = (position.z - cameraPosition.z) / mapHeight;
// We want howManyWidthsFromCamera to be between -0.5 to 0.5
if(howManyHeightsFromCamera > 0)
howManyHeightsFromCamera += 0.5f;
else
howManyHeightsFromCamera -= 0.5f;
int howManyHeightsToFix = (int)howManyHeightsFromCamera;
position.z -= howManyHeightsToFix * mapHeight;
}
return position;
}
}
Yeah, can you try cleaning your solution? It looks like something’s keeping intellisense from pathing through that property trail for the Camera class.
@Grinsel seriously?! how is this difficult? you cannot have a vector3 inside of a vector 3. thats why you have the error.
try getting rid of the “main”, i am unsure why its their and when i access a transform on a component I just go Camera.Transform.Position.
Though I have not seen main before, so it might be important.