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 SpontaneousIllusion · Nov 06, 2015 at 07:45 PM · c#guirigidbodyspawn

Try to reference Rigid Body

Hi I am try to reference the Rigid Body within my code, currently my code is attached to the Main Camera, and I have it so the player is able to move the character using the GUI Buttons.

alt text

However when I go onto actually spawn my character in by pressing the spawn button the character spawns, but the Move buttons do not make the duplicated character move?

The code I am using can be seen below

 using UnityEngine;
 using System.Collections;
 
 public class GUIControls : MonoBehaviour {
 
     public Transform prefab;
     public Rigidbody MalePirate;
 
     //public GameObject Cube;
     //public GameObject Cube2;
 
     void Start() {
         //Reference your player's rigidbody component at the "Start" of the game
         MalePirate = GameObject.Find("MalePirate").GetComponent<Rigidbody>();
     }
 
     void OnGUI()
     {
         if (GUI.Button (new Rect (10, 10, 150, 100), "Move The Cube Left")) 
         {
             MalePirate.AddForce (Vector3.right * 50);
             //Cube2.GetComponent<Rigidbody>().AddForce(Vector3.left*50);
         }
 
 
         if (GUI.Button (new Rect (200, 10, 150, 100), "Move The Cube Right")) 
         {
             MalePirate.AddForce (Vector3.left * 50);
             //Cube2.GetComponent<Rigidbody>().AddForce(Vector3.right*50);
         }
 
         if (GUI.Button (new Rect (400, 10, 150, 100), "Spawn Cube"))
         {
 
             {
                 Rigidbody.Instantiate (prefab, new Vector3 (-4, 0, 2), Quaternion.identity);
             }
         }
 
     }
 }

As well as this I am also getting the following error:

'NullReferenceException: Object reference not set to an instance of an object GUIControls.Start () (at Assets/Scripts/GUIControls.cs:14)'

And I have no idea what I need to reference or attach as for as far as I can see everything is already attachedalt text

Any help would be appreciated greatly, by the way yes I have tried googling I have been doing so for the past 2 days, but still can not see the issue.

unity-side.png (49.5 kB)
game.png (17.5 kB)
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 jeremy11 · Nov 07, 2015 at 03:39 AM

Ok well if i am understanding what you want to do is be able to spawn more of those pirates and then move all the pirates.

  1. make a empty game object

  2. Drag the priate and or cube under the empty game object (Parent it)

So heres some code

 var EmptyGameObject : GameObject;
 //GameObject EmptyGameObject;
 function OnGui(){
 if (Move Left Button) {
 EmptyGameObject.transform.position.x -= 1;
 }
 if (Move Right Button) {
 EmptyGameObject.transform.position.x += 1;
 }
 if (Spawn) {
 var Obj : GameObject = Instantiate(Crap in here);
 Obj.transform.parent = EmptyGameObject;
 }
 }



Now they should move together

Comment
Add comment · Show 2 · 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 SpontaneousIllusion · Nov 08, 2015 at 07:48 PM 0
Share

O$$anonymous$$ so just been trying what you put however I'm not sure where exactly that code needs to go? Does it go within the Code I already got or as a separate script?

avatar image jeremy11 · Nov 11, 2015 at 10:40 PM 0
Share

Well it should go into your current script but read this http://docs.unity3d.com/ScriptReference/Transform-parent.html

its about parenting(Sounds weird) when you parent an object to another the parent object will move a child objects so when you instantiate a new object simply set its parent to the object that moves like so

 newObject.transform.parent = $$anonymous$$ainobject.transform;

If you still need help ill rewrite your script with comments explaining everything.

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

Instantiated in IF statement - Else Reference object 1 Answer

hi guys am having a problem with my player 0 Answers

How to move forward (towards z), ignoring any rotations? 0 Answers

Float speed not getting updated on FixedUpdate nor Updated and OnAnimatorMove 1 Answer

Rigidbody script throwing error CS0246: The type or namespace name `MouseLook' could not be found. Are you missing an assembly reference? 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