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
1
Question by RomanorumLegio · Jun 09, 2013 at 05:15 AM · c#getcomponentvariables

Calling variable accross scripts in C# problem (Closed)

I have a public variable called powerUnits that I wish to be used across a variety of scripts. For the particular script I am writing, I want to check the availability of the variable then reduce it when the space bar is pressed. This should then have it fire 10 bullets.

The problem is that nothing happens at all. There is no firing nor depletion of units. What is more puzzling is that no errors are shown in the console. I have assigned what object to instantiate as well as the object to take location and orientation from. The script was modified from a previous script that I wrote that did not take powerUnits into account, and that worked without any problems.

I'm new to programming, so I greatly appreciate the help. If possible, please help me in terms of C#, I'm still learning the language, and I don't feel too ready to add learning java to the equation. Maybe in a month or two...

Here is my problem script: using UnityEngine; using System.Collections;

 public class Bullet1Fire : MonoBehaviour {
 
     public Rigidbody bullet;
     public Transform barrel1End; 
     private int wait = 25;
     TransformFunctions transformFunctions;
     
     void Awake(){
         transformFunctions = GetComponent<TransformFunctions>();
     }
     
     // Update is called once per frame
     void Update () {
         //Check powerUnit count from TransformFunctions script
         if(transformFunctions.powerUnits >= 100){
             //check for spacebar down
             if(Input.GetKeyDown(KeyCode.Space)){
                 //lower powerUnits
                 transformFunctions.powerUnits -= 100;
                 //Fire ten bullets
                 while(wait>=0){
                     Rigidbody bulletInstance;
                     bulletInstance = Instantiate(bullet, barrel1End.position, barrel1End.rotation) as Rigidbody;
                     bulletInstance.AddForce(barrel1End.forward * 5000);
                     wait --;
                 }
         
             }
         }
     }
 }

Here is a renamed version of the original working code: using UnityEngine; using System.Collections;

 public class Bullet2Fire : MonoBehaviour {
 
     public Rigidbody bullet;
     public Transform barrel1End; 
     private int wait = 50;
     TransformFunctions transformFunctions;
     // Update is called once per frame
     void Update () {
         
         if(wait == 0){
             if(Input.GetKey(KeyCode.Space)){
                 Rigidbody bulletInstance;
                 bulletInstance = Instantiate(bullet, barrel1End.position, barrel1End.rotation) as Rigidbody;
                 bulletInstance.AddForce(barrel1End.forward * 5000);
                 wait --;
             }
             }
         
     }
 }


Thanks in advance, and I'm sorry for any sloppy techniques!

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
1
Best Answer

Answer by Fattie · Jun 09, 2013 at 06:02 AM

You can very easily solve your problem by adding

Debug.Log();

statements.

void Awake() { transformFunctions = GetComponent(); transformFunctions.test(); }

You did not show us your file TransformFunctions but add this function to it ..

void Test() { Debug.Log("I am in 'test' "); }

Please be sure to TICK any useful answer, thanks.

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

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

15 People are following this question.

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

Related Questions

Using a variable value with GetComponent 1 Answer

fix script to get C# var from another object instead of object this script is attached to 2 Answers

C# Access to a variable by another script attached in a gameObject in the scene. 2 Answers

Syntax troubles (GetComponent and changing variables) 1 Answer

Getting a Variable on Script B to equal a variable from script A 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