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
0
Question by Yinoguns · Oct 04, 2013 at 06:28 PM · 2darrayvector3method

2D Array and other Script method access

Hello, been trying to sort out this code for some time and I keep getting the same few errors:

Assets/Standard Assets/Scripts/MY Scripts/GetFacing.cs(158,40): error CS0120: An object reference is required to access non-static member GetFacing.FacingMul' Assets/Standard Assets/Scripts/MY Scripts/GetFacing.cs(158,65): error CS0120: An object reference is required to access non-static member GetFacing.FacingMul' Assets/Standard Assets/Scripts/MY Scripts/GetFacing.cs(158,89): error CS1502: The best overloaded method match for UnityEngine.Vector3.Vector3(float, float, float)' has some invalid arguments Assets/Standard Assets/Scripts/MY Scripts/GetFacing.cs(158,89): error CS1503: Argument #1' cannot convert object' expression to type float'

What I have been trying to sort out is after I have worked out (in GetFacing) what way the player is facing, I use the 2D array FacingMul with the int facing, to return the appropriate multipliers rather than use multiple if statements.

This is then used in another Script which spawns a model at a X and Z cord multiplied on what comes from the PosMulFacing via Vector3.scale, the Y is always 0 as I am not changing the height.

Below is the code for the GetFacing script, please note C# only please.

 using UnityEngine;
 using System.Collections;
 
 public class GetFacing : MonoBehaviour {
     
     
     private float curRot;
     
     private float best = 400;
     
     private int facing = 0;
     
     //0 = x, 1 = z
     //private int[,] FacingMul = new int[2,4];
     
     int[,] FacingMul = new int[2,4];
     /*
     {
         { 0,-1}, //-Z
         {-1, 0}, //-X
         { 1, 1}, // Z
         { 1, 0}  // X
     };
     */
     
     /*
     {
         { 0,-1}, //-Z
         {-1, 0}, //-X
         { 1, 1}, // Z
         { 1, 0}  // X
     };//Array
     */
         
     private static float negZ = 180,
                          negX = 270,
                             posZ = 360,
                          posX = 90;
     
     private float[] RotValues = new float[4];// = new float[4] {negZ,negX,posZ,posX};
     /*      
      * -Z: 180        0
      * -X: 270        1
      *  Z: 0/360    2
      *  X: 90         3
      */
     
     
     //do some borderline testing with excel? or processing
     
     
     //-------------------------------------------------------------
     // Use this for initialization
     void Start () {
         
         RotValues[0] = negZ;
         RotValues[1] = negX;
         RotValues[2] = posZ;
         RotValues[3] = posX;
         
         FacingMul[0,0] = 0;    FacingMul[1,0] = -1;
         FacingMul[0,1] = -1;FacingMul[1,1] = 0;
         FacingMul[0,2] = 1;    FacingMul[1,2] = 1;
         FacingMul[0,3] = 1;    FacingMul[1,3] = 0;
         
         /*
         {
         { 0,-1}, //-Z
         {-1, 0}, //-X
         { 1, 1}, // Z
         { 1, 0}  // X
     };//Array
     */
         
         for( int x=0; x<4; x++){
             for( int y=0; y<2; y++){
                 
                 //Debug.Log("FacingMul["+x+"]["+y+"] = "+FacingMul[x][y]);
                 
             }
         }
         
         
         float  curRot = transform.eulerAngles.y;
         
         
         if( curRot < 180 ){
             posZ = 0;
         }else
         if( 180 < curRot ){
             posZ = 360;
         }
         
         
     
     }//Start
     
     
     //-------------------------------------------------------------
     // Update is called once per frame
     void Update () {
         
         best = 400;
         
         float  curRot = transform.eulerAngles.y;//Get Rot
         
         //int facing = 0;
     
         for(int c=0; c<4; c++){
             
             float disToRot = Mathf.Abs(        Mathf.DeltaAngle( curRot, RotValues[c] )    );
             
             //Debug.Log("c: "+c+", best prior: "+best);
             
             if( disToRot < best ){
                 
                 best = disToRot;
                 
                 //test code
                 facing = c;
             }//if
             
             //need to check for new best
             
             //Debug.Log ("c: "+c+", best after: "+best);
             //Debug.Log ("Dis between ("+curRot+"-"+RotValues[c]+" = "+Mathf.DeltaAngle( curRot, RotValues[c] )+")" );
             
         }//for
         
         
         
         
         
         if(facing == 0){
             Debug.Log("facing: -Z");
         }else
         if(facing == 1){
             Debug.Log("facing: -X");
         }else
         if(facing == 2){
             Debug.Log("facing:  Z");
         }else
         if(facing == 3){
             Debug.Log("facing:  X");
         }else{
             Debug.Log("facing errored");
         }//if direction
         
         
     
     }//Update
     
     
     //---------------------------------------------------
     //Return Vec3 as returning for a position
     public static Vector3 PosMulFacing(){
         
         return(   new Vector3( FacingMul[0,facing] ,0f, FacingMul[1,facing] )    );
             
     }//PosMulFacing
     
 }//CLASS
 

The following code and script works fine, but this is the only bit that interacts with the above Script. If I am trying to access the other scripts method wrong, please shout.

 pos = Vector3.Scale( _GetFacing.PosMulFacing(), new Vector3(10,10,10)    );
                 
             switch(index){
             case 0:
                 Instantiate(room1, pos,  Quaternion.identity);

Thanks, I am going to continue to try and work this out.

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 Yinoguns · Oct 04, 2013 at 06:24 PM 0
Share

Errors got squished together, here they are spaced out, sorry.

Assets/Standard Assets/Scripts/$$anonymous$$Y Scripts/GetFacing.cs(158,40): error CS0120: An object reference is required to access non-static member GetFacing.Facing$$anonymous$$ul'

Assets/Standard Assets/Scripts/$$anonymous$$Y Scripts/GetFacing.cs(158,65): error CS0120: An object reference is required to access non-static memberGetFacing.Facing$$anonymous$$ul'

Assets/Standard Assets/Scripts/$$anonymous$$Y Scripts/GetFacing.cs(158,89): error CS1502: The best overloaded method match for UnityEngine.Vector3.Vector3(float, float, float)' has some invalid arguments

Assets/Standard Assets/Scripts/$$anonymous$$Y Scripts/GetFacing.cs(158,89): error CS1503: Argument#1' cannot convert object' expression to typefloat'

avatar image Yinoguns · Oct 04, 2013 at 06:46 PM 0
Share

Only have 1 error now:

Assets/Standard Assets/Scripts/$$anonymous$$Y Scripts/SpawnRoomPiece.cs(40,48): error CS0120: An object reference is required to access non-static member GetFacing.Pos$$anonymous$$ulFacing()' I made the following changes: Vector3 V3 = GetFacing.Pos$$anonymous$$ulFacing();//new Vector3(x,y,z+(10*dis) ); //Vector3 pos = _GetFacing.Pos$$anonymous$$ulFacing(); pos = Vector3.Scale( V3, new Vector3(10,10,10) ); // Pos$$anonymous$$ulFacing is not Static, but doing so produces these errors: Assets/Standard Assets/Scripts/$$anonymous$$Y Scripts/GetFacing.cs(**158,40**): error CS0120: An object reference is required to access non-static member GetFacing.Facing$$anonymous$$ul'

Assets/Standard Assets/Scripts/$$anonymous$$Y Scripts/GetFacing.cs(158,65): error CS0120: An object reference is required to access non-static member GetFacing.Facing$$anonymous$$ul' Assets/Standard Assets/Scripts/$$anonymous$$Y Scripts/GetFacing.cs(158,89): error CS1502: The best overloaded method match for UnityEngine.Vector3.Vector3(float, float, float)' has some invalid arguments

Assets/Standard Assets/Scripts/$$anonymous$$Y Scripts/GetFacing.cs(158,89): error CS1503: Argument #1' cannot convert object' expression to type `float'

1 Reply

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

Answer by jacksmash2012 · Oct 04, 2013 at 06:44 PM

You are trying to access FacingMul, which is a member of the class GetFacing, but you are trying to access it from a static context, namely the PosMulFacing() function. You cannot do this. You can solve this in a couple of ways:

  1. Do not make PosMulFacing() static and require an instance of GetFacing to call that function.

  2. Create a singleton instance (controversial, yes) of GetFacing so that you can access the FacingMul array from within the static function. Something like:

public static GetFacing instance = null;

and then in the Awake() function, have something like:

instance = this;

And then in your static PosMulFacing() you could do GetFacing.instance.FacingMul

Hope this helps.

Comment
Add comment · Show 5 · 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 Yinoguns · Oct 04, 2013 at 06:48 PM 0
Share

The first solution sounds more preferable, but I have tried in SpawnRoomPiece the script to call it, to grab the script is that not instantiating it?

Im a University student but have not come across this issue much, only recently been crossing paths with statics.

avatar image jacksmash2012 · Oct 04, 2013 at 06:51 PM 0
Share

I'm not sure what your first sentence means, but in your SpawnRoomPiece script make sure you have a serialized instance of GetFacing, and then you can call any public function in the GetFacing class. So in SpawnRoomPiece you could have:

[Serializable] private GetFacing getFacingScript;

and then assign that in the editor, and then you can call the Pos$$anonymous$$ulFacing() function.

avatar image Yinoguns · Oct 04, 2013 at 07:08 PM 0
Share

Sorry about the first sentence, I am english just disability sometimes my sentences don't make sense, bleh :D

I worked it out, I did GetComponent(); but I didn't declare the "variable" up the top, so late night idiot coding.

Thanks for the help anyway.

avatar image jacksmash2012 · Oct 04, 2013 at 07:10 PM 0
Share

Ok, please accept and up-vote my answer if it was helpful to you.

avatar image Yinoguns · Oct 04, 2013 at 07:12 PM 0
Share

Need a Rep of 15 to up vote im afraid, thanks for the help.

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

16 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

Related Questions

Multiple Fire Points 2 Answers

How to move an object and keep its distance relative to the player? 2 Answers

C# Convert Vector3[] to Vector2[] 3 Answers

Mathf.Round inside method behaves different 1 Answer

How to use SimpleMove randomly move an NPC? 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