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.
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 attached
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.
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.
make a empty game object
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
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?
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.