- Home /
Cant access variable with GetComponent<>();
I read some help but i cant access variable on my other script.`using UnityEngine; using System.Collections;
public class ShootMove : MonoBehaviour {
// Use this for initialization
void Start () {
Networking123.realPos = Move.hlaven.transform.position;
if (Move.facingRight == false) {
rigidbody2D.velocity = new Vector3 (15f, 0, 0);
} else {
rigidbody2D.velocity = new Vector3 (-15f, 0, 0);
}
}
// Update is called once per frame
void Update () {
}
void OnCollisionEnter2D (Collision2D col){
if (col.gameObject.tag == "Player") {
GameObject resp = col.gameObject;
Respawning a = resp.GetComponent<Respawning>();
a.resptime = Time.realtimeSinceStartup;
PhotonNetwork.Destroy (col.gameObject);
PhotonNetwork.Destroy (this.gameObject);
}
else {
PhotonNetwork.Destroy (this.gameObject);
}
}
}`
I have my variable resptime in other script as "public float resptime = 0f;" Its on other object and i want to write to it when its colide with my player. When you can please write the example of code to do this :) i dont speak english very well so some references are usefull but something is not understand for me.
Thanks Dejvidus
"IF" you don't have any errors, you can simply add a Debug.Log() to check if you are actually entering OnCollisionEnter2D() function , and , if so, check the value you assign to resptime.
If you do have errors, tell us what they are.
There is nothing wrong with this code, you need to provide us with the error before we can help you out. It can be a million reasons why you can't get access to your variable, this code is not the reason.
using UnityEngine; using System.Collections; using Photon;
public class Respawning : Photon.$$anonymous$$onoBehaviour {
static public float resptime = 0f;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (resptime > 10) {
GameObject já = PhotonNetwork.Instantiate("Cube", new Vector3 (0, 0, 0), transform.rotation, 0);
((Photon.$$anonymous$$onoBehaviour)já.GetComponent ("$$anonymous$$ove")).enabled = true;
((Photon.$$anonymous$$onoBehaviour)já.GetComponent ("Shooting")).enabled = true;
Rigidbody2D gameObjectsRigidBody = já.AddComponent<Rigidbody2D>();
gameObjectsRigidBody.mass = 2;
gameObjectsRigidBody.fixedAngle = true;
resptime = 0;
}
}
}
there is the second code. Whatfrom i want to access the resptime. I dont know where can be the fail. :/
Assets/PhotonZkouska/Assets/Resources/Shoot$$anonymous$$ove.cs(24,27): error CS0176: Static member `Respawning.resptime' cannot be accessed with an instance reference, qualify it with a type name ins$$anonymous$$d
Im sorry i didnt write the error. There is it. :)
Answer by Gogeric · Apr 08, 2015 at 11:57 AM
As per your error you need to call resptime this way :
Respawning.resptime = Time.realtimeSinceStartup;
you dont need to use GetComponent or even to query the game object
Your code could be :
void OnCollisionEnter2D (Collision2D col){
if (col.gameObject.tag == "Player") {
Respawning.resptime = Time.realtimeSinceStartup;
PhotonNetwork.Destroy (col.gameObject);
}
PhotonNetwork.Destroy (this.gameObject);
}
That is because resptime is declared static which means you can access it directly and it is not tied to an instance of the Respawning class.
A few consideration as a bonus :
Do not name your classes using a Verb like "Respawning", it is misleading for the reader
Avoid static fields on classes that will be instanciated, in some very rare case you might need that but for all other cases there probably exist a better solution
Do not duplicate code like
PhotonNetwork.Destroy (this.gameObject);
you had one more operation for theelse
and it will bring tons of errors when you will come back to this code to add new features.
Thanks so much, but i have one more question. if the script "respawning" is on this object and on other object too, will i access the variable from other gameObject ? I thougt i will change variable on this gameObject but i need change variable on gameobject what i collided and script what i got (by GetComponent)
If your script is on two objects at the same time (we call that "instances") it does not matter your variable will still be available even if all your instances are destroyed, because that variable is static, it also means it is always the same for all the instances at any time
If you want all instances to have different values for resptime
the resptime needs to not be static, then you can do what you did at the beginning and the code will work
Okay thats seems to be better for me, when i use just "public float resptime". That was saying me some error around non-static member but i rewrote the code and now its good. Thank you so much :)
Okay there is one more problem, when i build it and play that say "NullReferenceException: Object reference not set to an instance of an object Shoot$$anonymous$$ove.OnCollisionEnter2D (UnityEngine.Collision2D col) (at Assets/PhotonZkouska/Assets/Resources/Shoot$$anonymous$$ove.cs:24)" that is on 24 line .... a.resptime = Time.realtimeSinceStartup; i dont understand it why it failed.
No problem, mark the error as resolved if it answered your question, that might help other people passing by
Your answer
