why does "Camera.main.transform.position" not work?
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 );
}
}
This is weird, Camera.main.transform.position
is correct syntax, and should be accessible. The possible issues I can think of:
- you have created a Camera
class of your own, so it and the one from UnityEngine
are conflicting
- you have an unusual character that looks like a normal one, but it is not the same, e.g. a .
or an i
character that looks like a normal character, but has a higher unicode value (the typical example of this is ;
semicolon and ;
Greek question mark). You can check this if you open the file with a text editor that can visualize hex values
- the first parameter to Hex.PositionFromCamera()
is not a Vector3
These are all the ideas that come to my $$anonymous$$d.
Hey can you please the Script definition for Hex? not sure i've seen the class before.
Answer by Freeliner · Jun 30, 2019 at 08:01 AM
Very late answer, but for those who stumbled on the same problem: In documentation (https://docs.unity3d.com/ScriptReference/Camera-main.html) 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.
Answer by $$anonymous$$ · Jul 03, 2018 at 09:06 AM
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.
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
);
This won't work, as the Camera
class wouldn't know which specific Transform to use. main
is the property that returns an actual instance of Camera
which will have a transform
attached.
Answer by Grinsel · Jul 03, 2018 at 10:48 AM
@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;
}
}
@Grinsel I just looked up what camera.main is. I believe I thought up a possible solution.
I have not tried this myself, so might not work but its worth a try.
Public GameObject CameraUnder;
GameObject Camera = CameraUnder.$$anonymous$$ain;
CameraUnder should be set to your camera
Then to do the thing you want to do with your camera, you could do this:
Camera.Transform.Position;
I've just copied your code into a project, and it doesn't give any errors, which suggests something got stuck in memory.
I recommend incrementally cleaning and restarting elements: first the source code editor (possibly deleting .sln and .csproj files), then Unity (might want to delete Libraries/ and Temp/ folders), and finally, Windows itself (this might not affect anything).
You might also try to copy your code into a new, empty project, and see what happens. Finally, you can try to reinstall Unity (but I don't think this would solve the problem).
i use a linux version of unity ( 5.6.0xb1Linux Personal 64bit ) (not easy to find a download able newer one)
i'll try to rebuild it from scratch, the code is also tested by another windows user. maybe my project itself is anyhow corrupted.
as beginner, i am happy with all ur help and ideas. after rebuild, i give u all a update.
after i rebuild it now from scratch, the error is gone... i made any mistake, no idea what.
Yeah, can you try cleaning your solution? It looks like something's keeping intellisense from pathing through that property trail for the Camera class.