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 Ikunze · May 03, 2012 at 05:21 AM · guiinstantiategui-button

Look for object, instantiate if not there, and finally add an impulse

Hi. I'm brand new to Unity, and I've been trying to glean what I can from the manual and trolling other questions, but haven't found anything that quite fits what I want to do.

In any case, I'm trying to create a pretty simple shooter game, something like Tanks http://www.mathsisfun.com/games/tanks.html but in 3d.

----------------------------What I'm Trying----------------------------------

I'm trying to use a GUI slider to set power, then use a GUI button shoot the projectile. In my initial experiments, I had a slider script, a button script, and a script to add an impulse to a game object. What I can't get working is projectile instantiation, and then adding an impulse when the button is pressed. The three worked together well in a different scene where I pushed a boat. Now, I dunno what's going on.

----------------------------What I Would Like--------------------------------

I want it to look for the projectile, and if it doesn't exist the in the scene, instantiate at the barrel. Then, when I press the GUI button, apply an impulse to the projectile according to the slider value.

Alternatively, I would be happy if I could receive button-down on the GUI button, and instantiate the projectile with an initial velocity according to the slider.

----------------------------The Issue----------------------------------------

My projectile won't instantiate. Also, the physics of the collider are weird. I made a "floor" of a giant, squashed cube, and put a cylinder collider on the projectile. When I was getting it to instantiate earlier (though the impulse wasn't working, because I didn't think ahead for the null case, and instead the slider controlled the frequency instantiation) the bloody things kept falling through the cube/floor & would phase half through the floor and roll, while some would pass right through it.

----------------------------Scripts So Far------------------------------------

Slider:

     #pragma strict
 
 static var hSliderValue : float = 0.0;
 
 function OnGUI () {
     hSliderValue = GUI.HorizontalSlider (Rect (25, 150, 100, 30), hSliderValue, 0.0, 10000000.0);
     
     GUI.Label (Rect (25, 155, 100, 30), hSliderValue.ToString());
     
 }

Button:

 #pragma strict
 
 static var alittlepush= 0;
 
 function OnGUI () {
     if (~alittlepush) {
     alittlepush=0;
     }
 
     if (GUI.Button (Rect (260,73,200,100), "I am a button")) {
         print ("You clicked the button!");
         alittlepush = 1;
     }    
 }

Impulse drive: #pragma strict

 var projectile: GameObject;
 
 function Update () {
 if (I_am_a_button.alittlepush) {
 var power: Vector3 = Vector3(0,0, make_a_slider.hSliderValue);
 
     projectile.rigidbody.AddForce(power, ForceMode.Impulse);
     }
 
 }


I've tried something like this:

     #pragma strict
 
 var Origin : Transform;
 var respawnPrefab : GameObject;
 //var respawn = GameObject.Find("cannon_shell");
 function Update () {
     var respawn = GameObject.Find("cannon_shell");
     if (respawn !=null ) {
         Instantiate (respawnPrefab, Origin.transform.position, Origin.transform.rotation);
         }
         
     if (I_am_a_button.alittlepush) {
     var power: Vector3 = Vector3(0,0, powerSlider.powerValue);
 
         respawnPrefab.rigidbody.AddForce(power, ForceMode.Impulse);
     }        
 }

Originally, I was getting an error, "Argument Exception: Find can only be called from the main thread," so I moved the Find variable inside the Update function. For the exposed variables: I made the barrel "Origin" and the respawnPrefab "cannon_shell," a prefab containing a 3d model of a British anti-aircraft shell I got off blender.

Now, it just flat-out refuses to Instantiate the shell. Halp?

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

Answer by Berenger · May 03, 2012 at 05:50 AM

Splitting up your code in several files like that had you confused. The issue here is the Unity function execution order. OnGUI is called after Update at the end of the frame and several times before the next Update, so alittlepush might be reset.

I suggest you gather slider, button, instantiation and force in one OnGUI function.

Comment
Add comment · Show 1 · 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 Ikunze · May 04, 2012 at 12:29 AM 0
Share

.< Thanks!

I'll redo it and post again with a (hopefully) working script. $$anonymous$$y only program$$anonymous$$g experience before this is some basic $$anonymous$$ATLAB, where I prefer keeping things in multiple scripts according to function in order to organize them. It seems that for unity/javascript it does better to keep large blocks of code in a single file to make sure the runtime execution goes smoothly, and each of the different functions can access variables & events without declaring global vars. Is it in fact better program$$anonymous$$g practice to keep larger files which track multiple things?

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

Using a GUI.Button to Instantiate gameObject using another gameObject's location 1 Answer

How to toggle the color of a button? 1 Answer

Making a Hover function for a button or toolbar from code. 2 Answers

Ensuring Correct Call Order 0 Answers

Close my GUI button by repressing the same Hot-key. 3 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