- Home /
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.
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'
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'
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:
Do not make PosMulFacing() static and require an instance of GetFacing to call that function.
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.
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.
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.
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.
Ok, please accept and up-vote my answer if it was helpful to you.
Need a Rep of 15 to up vote im afraid, thanks for the help.
Your answer
Follow this Question
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