Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by root_2 · May 30, 2016 at 09:21 AM · c#rigidbodies

RigidBody_problem with gameobject

Hello, I have a little problem. I want to instanciate an GameObject (Sphere) at runtime to be on my terrain. I used the Instanciate method but when i want to create that sphere at runtime, a sphere appears twice or i want a sphere to appear once. Do you have any ideas ? here is my code

 using UnityEngine;
 using System.Collections;

public class create_marble : MonoBehaviour {

  public GameObject sphere;
  public Camera camera;    
  void OnGUI()
       {
     if (Input.GetKeyDown(KeyCode.E))
     {
         Vector3 pos = camera.transform.position + camera.transform.forward * 5f;
         //sphere.transform.localScale += new Vector3(2, 2, 2);    
         Instantiate(sphere, pos, Quaternion.identity);

         
         
         
     }
 }

}

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

2 Replies

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

Answer by Veerababu.g · May 31, 2016 at 04:41 AM

use getkeydown instead of get key.get key down works only one time but get key works upto you release the key. it's like step. and one more thing use different different position to instantiate. if you are new to unity then take look at unity tutorials

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 root_2 · May 31, 2016 at 11:13 AM 0
Share

thanks it's working with getkeydown .well i want to instantiate the object in front of the camera. and i guessed this is the best way to do what i want by using Vector3 pos = camera.transform.position + camera.transform.forward * 5f; isnt it ?

avatar image
0

Answer by root_2 · May 30, 2016 at 09:36 AM

here is a screen capture of my problemalt text


unity-pb.jpg (183.8 kB)
Comment
Add comment · Show 4 · 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 Veerababu.g · May 30, 2016 at 10:16 AM 1
Share

may i know is there any reason to use void OnGUI

avatar image root_2 Veerababu.g · May 30, 2016 at 11:09 AM 0
Share

Oh right ..... i changed the void onGUI() and put void Update and it works fine for me

avatar image Veerababu.g · May 30, 2016 at 11:42 AM 0
Share

then accept the answer

avatar image root_2 Veerababu.g · May 30, 2016 at 12:10 PM 0
Share

I spoke too fast it works with one object but if i want to add more object like (cube,capsule,cylinder).Like when i press the button for the cube the local_size is 10 times bigger than what i expected and the rigid_body make the object appears a lot. I modify the code with the Update method ins$$anonymous$$d of the OnGUI(); here is the modify version of the code

  using UnityEngine;
  using System.Collections;


 public class create_marble : $$anonymous$$onoBehaviour {
 
 public GameObject sphere;
 public GameObject cube;   
 public GameObject Capsule;
 public GameObject cylinder;
 public Camera camera;    
  void Update()
 {
     if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.E))
     {
         Vector3 pos = camera.transform.position + camera.transform.forward * 5f;
         Instantiate(sphere, pos, Quaternion.identity);   
     }
     if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.H))
     {
         Vector3 pos = camera.transform.position + camera.transform.forward * 5f;
         Instantiate(cube, pos, Quaternion.identity);
     }
     if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.L))
     {
         Vector3 pos = camera.transform.position + camera.transform.forward * 5f;
         Capsule.transform.localScale += new Vector3(2, 2, 2);
         Instantiate(Capsule, pos, Quaternion.identity);
     }
     if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.P))
     {
         Vector3 pos = camera.transform.position + camera.transform.forward * 5f;
         cylinder.transform.localScale += new Vector3(2, 5, 2);
         Instantiate(cylinder, pos, Quaternion.identity);
     }


 }

}

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

166 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 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 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 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 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 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 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 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 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 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 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 avatar image

Related Questions

[Solved] How to Script a Camera That Follows the Player but Collides with the Edge of the Level 2 Answers

localscale not working 1 Answer

Camera Jitters When Displacing and Rotating Smoothly 0 Answers

My sprite disappears from the camera view after some time moving 5 Answers

NullReferenceException: Object reference not set to an instance of an objectInt32) 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