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 Etarnalazure · Sep 11, 2012 at 03:48 PM · c#gameobjectrigidbodyboolean

Getting variables from one class to another

Hello everyone, I've recently delved into Unity as a part of my class, we have been told to recreate a retro game throughout this month and the next.

HOWEVER!

I've been stuck for quite a while on how to edit a variable in another class then the one you're currently working in.

Normally I'd write "Class class;" and then have access to it just fine, however when ever i try and do this, it returns everything in my class as null.

How do i avoid this?

Heres the classes I've been toying around with:

 public Rigidbody bullet;
     public bool canMove = false;
     public float power = 1500;
     public float moveSpeed = 5.0f;
     
     // Use this for initialization
     void Start () 
     {
         //joint.connectedBody = GameObject.Find("physics_body").rigidbody;
         bullet = GameObject.Find("Cube").rigidbody;
     }
     
     // Update is called once per frame
     void Update () 
     {
         float h = Input.GetAxis("Horizontal") * Time.deltaTime * moveSpeed;
         float v = Input.GetAxis("Vertical") * Time.deltaTime * moveSpeed;
         
         transform.Translate(h, v, 0);
         
         
         if(canMove == true)
         {
             if(Input.GetButtonDown("Fire1"))
             {
                 
                 Shoot();
             }
         }
     }
     
     void Shoot()
     {
           
         Rigidbody instance = Instantiate(bullet, transform.position, transform.rotation) as Rigidbody;
             Vector3 fwd = transform.TransformDirection(Vector3.forward);
             instance.AddForce(fwd * power);
     }


This code basically just allows a player to choose in front of him/her, however since I've been trying to make a menu, this needs to be "paused" when ever the player is on the menu.

So i figured i could just do this:

 bool canMove = false;
     ShootBullet shoot;
     // Use this for initialization
     void Start () 
     {
     
     }
 
     // Update is called once per frame
     void Update () 
     {
         if(canMove == true)
         {
             shoot.canMove = true;
         }
         if(Input.GetKeyDown(KeyCode.B))
         {
             canMove = true;
         }
     }

But no, it just returns everything as null.

Hopefully someone can help me and point out where I've gone wrong.

Thanks.

Comment
Add comment
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

1 Reply

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

Answer by skalev · Sep 11, 2012 at 04:04 PM

If you want to pause the game, set Time.timeScale = 0; it will pause the game and not the gui.

However, make sure that you account for that in your scripts, and in Update() check if time.timeScale == 0, and don't do any processing if that is the case.

Comment
Add comment · Show 3 · 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 Etarnalazure · Sep 11, 2012 at 04:36 PM 0
Share

Thank you, thought it was like in XNA, so if you disrupted the timer, it would stop the entire game.

However I'd still like to know what i do wrong with calling the bools. I could just be a giant idiot and not see it myself.

avatar image skalev · Sep 11, 2012 at 05:16 PM 0
Share

It looks like you are not assigning anything to the shoot variable. so it makes sense that it is null.

avatar image Etarnalazure · Sep 11, 2012 at 05:26 PM 0
Share

Would make sense. Thank you.

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

9 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Why does IsSleeping() keep returning false? (Billiard Logic) 0 Answers

Desync between host and client. 0 Answers

GameObject variable doesn't get filled in 0 Answers

Accessing RigidbodyFirstPersonController 0 Answers

How can I define a joint for my ragdoll? 0 Answers


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