Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 /
  • Help Room /
avatar image
0
Question by Grinsel · Jul 03, 2018 at 08:15 AM · camera.main

why does "Camera.main.transform.position" not work?

alt text

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


Comment
Add comment · Show 2
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 Harinezumi · Jul 03, 2018 at 09:28 AM 0
Share

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.

avatar image Cornelis-de-Jager · Jul 03, 2018 at 11:12 AM 0
Share

Hey can you please the Script definition for Hex? not sure i've seen the class before.

4 Replies

· Add your reply
  • Sort: 
avatar image
3

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.

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
-1

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.

Comment
Add comment · Show 2 · 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 Grinsel · Jul 03, 2018 at 09:49 AM 0
Share

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


avatar image TreyH · Jul 03, 2018 at 11:27 AM 0
Share

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.

avatar image
0

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


alt text

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

Comment
Add comment · Show 6 · 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 $$anonymous$$ · Jul 03, 2018 at 10:55 AM 0
Share

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


 
 

avatar image Harinezumi · Jul 03, 2018 at 11:24 AM 0
Share

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

avatar image Grinsel Harinezumi · Jul 03, 2018 at 11:53 AM 1
Share

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.

avatar image Grinsel Grinsel · Jul 03, 2018 at 01:39 PM 1
Share

alt text after i rebuild it now from scratch, the error is gone... i made any mistake, no idea what.

Show more comments
avatar image TreyH · Jul 03, 2018 at 11:39 AM 0
Share

Yeah, can you try cleaning your solution? It looks like something's keeping intellisense from pathing through that property trail for the Camera class.

alt text

reload.gif (101.4 kB)
avatar image
0

Answer by samf1111 · Jun 27, 2020 at 10:33 AM

@Grinsel seriously?! how is this difficult? you cannot have a vector3 inside of a vector 3. thats why you have the error.

Comment
Add comment · 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

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

90 People are following this question.

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

Related Questions

Problems using Camera.main.ScreenToViewportPoint(Input.mousePosition) as it gives the wrong position 0 Answers

Camera zoom out on follow 1 Answer

Why does nothing happen on the game screen when i press play?? 0 Answers

Main camera field of view (FOV) angles in inspector doesn't match to actual used FOV of the main camera 0 Answers

Unity 5 c# - Cannot change between multiple cameras 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