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 DejvidusX · Apr 08, 2015 at 09:58 AM · variablegetcomponentaccessing scripts

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

Comment
Add comment · Show 4
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 Nerevar · Apr 08, 2015 at 10:07 AM 0
Share

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

avatar image winsjansen · Apr 08, 2015 at 11:36 AM 0
Share

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.

avatar image DejvidusX winsjansen · Apr 08, 2015 at 11:48 AM 0
Share

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. :/

avatar image DejvidusX · Apr 08, 2015 at 11:43 AM 0
Share

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

1 Reply

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

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 the else and it will bring tons of errors when you will come back to this code to add new features.

Comment
Add comment · Show 8 · 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 DejvidusX · Apr 08, 2015 at 12:37 PM 0
Share

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)

avatar image Gogeric · Apr 08, 2015 at 12:44 PM 0
Share

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

avatar image DejvidusX · Apr 08, 2015 at 12:49 PM 0
Share

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

avatar image DejvidusX · Apr 08, 2015 at 01:27 PM 0
Share

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.

avatar image Gogeric · Apr 08, 2015 at 01:27 PM 1
Share

No problem, mark the error as resolved if it answered your question, that might help other people passing by

Show more comments

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Is it possible to set variables for classes? 2 Answers

Accessing variables in another script using GetComponent() 0 Answers

Problem with 2 scripts communicating 0 Answers

When editing a scripts variable in another script, does the original script update? 2 Answers

Can't understand generic getComponent for Js 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