- Home /
accessing instance of a script
Hello,
I wrote the following script to store the initial position of few gameobjects. The script is attached to each gameobject :
using UnityEngine;
using System.Collections;
public class ProprietePieces : MonoBehaviour
{
public static Vector3 PositionInitialePiece;
// Use this for initialization
void Start()
{
PositionInitialePiece = gameObject.transform.position;
}
}
My problem is that I do not succeed to access the "PositionInitialePiece" variable from another script. Here is what I use :
ProprietePieces ScriptProprietePieces = gameObject.GetComponent<ProprietePieces>();
Debug.Log(ScriptProprietePieces.PositionInitialePiece);
I get an error message saying that "Member ProprietePieces.PositionInitialePiece is not reachable with an instance reference, use a type name"
I do not understand what is the problem, and hope someone could help me.
Answer by Wuzseen · Jun 26, 2013 at 03:05 PM
Your positioninitialepiece is a static variable.
In C# you can't access a static member as if it were a regular instance of a class. This isn't the case in Java I believe, which may be what you're familiar with I'm guessing.
Simply drop the static in your ProprietePieces PositionInitialePiece variable and your code should be fine.
Your answer
![](https://koobas.hobune.stream/wayback/20220613104911im_/https://answers.unity.com/themes/thub/images/avi.jpg)