- Home /
2d side scrolling problem
hello fellas i want create 2d side scrolling shooter game but i have problem
when i instansiate projectile it is not move
how can make player dont go out of camera
how can when projectile go out of camera die
i upload my project please explain with detail from my project
http://www.4shared.com/rar/CH8JudxC/Invasion.html
Later ...
This my code ,i add rigidbody to projectie and add if to projectile destroy after out of screen but when it destroy it will not generate again by instatiate
"MissingReferenceException: The object of type 'GameObject' has been destroyed but you are still trying to access it. Your script should either check if it is null or you should not destroy the object."
this my projectile code :
public class Projectile : MonoBehaviour {
// Update is called once per frame
void Update ()
{
gameObject.rigidbody.velocity = new Vector3(0, 0, 22);
if (gameObject.transform.position.z > 23)
{
Destroy(gameObject);
}
}
}
and this is player code that attatch projectile that turn it to prefab
public GameObject prjectileprefab;
void Start () { }
void Update ()
{
if (Input.GetKey(KeyCode.S))
{ gameObject.transform.Translate(Vector3.down * 10*Time.deltaTime); }
if (Input.GetKey(KeyCode.W))
{ gameObject.transform.Translate(Vector3.up * 10 * Time.deltaTime); }
if (Input.GetKey(KeyCode.A))
{ gameObject.transform.Translate(Vector3.forward * 10 * Time.deltaTime); }
if (Input.GetKey(KeyCode.D))
{ gameObject.transform.Translate(Vector3.back * 10 * Time.deltaTime); }
if (Input.GetKeyDown(KeyCode.Space))
{
Instantiate(prjectileprefab, new Vector3(gameObject.transform.position.x,
gameObject.transform.position.y, gameObject.transform.position.z), Quaternion.identity);
}
}
}
Thanks for any help
If you post code, take a moment to FOR$$anonymous$$AT IT VERY NEATLY.
Nobody wants to look at crap code formatting.
$$anonymous$$ake sure EVERY LINE is perfectly formatted, and do not have any wasted space.
Cheers
Answer by robertofantasy · Aug 05, 2012 at 06:22 PM
someone important delete this, I moved it also
The reason why no one is looking at your code is because it is hosted somewhere else and we would download a rar file...just copy paste here and we will look at it.
Answer by robertofantasy · Aug 06, 2012 at 11:03 AM
Someone important erase this, I moved it to the question
and please don't post comments as answers, just edit your original post
Answer by Seth-Bergman · Aug 06, 2012 at 11:42 AM
you need to either add a rigidbody, or change the line:
gameObject.rigidbody.velocity = new Vector3(0, 0, 22);
to something that doesn't use a rigidbody, like:
transform.Translate(0,0,22);
the line
gameObject.rigidbody.velocity = new Vector3(0, 0, 22);
is trying to access a rigidbody component
you can move an object via its transform if you are Not interested in physics
If it's a character, you can add a Character Controller, and then use
controller.SimpleMove(Vector3.forward);
it just depends, there are lots of ways to move objects, you just need to research transform or character controller
as for dying once it's off camera, you could use:
if (!renderer.isVisible)
Destroy(gameObject);
does that help?
EDIT (for new issue): try
var clone = Instantiate(prjectileprefab, new Vector3(gameObject.transform.position.x,
gameObject.transform.position.y, gameObject.transform.position.z), Quaternion.identity);
guessing that may work
you should create a prefab assets > create > prefab
and use this as your prefab, not an object in the scene
i make prefab from my projectile
and the question is first why you assign instantiate to a clone
the second question that it is my main question when the prefab destroy when i want insatantiate base on my code that was with space key it not generate anything While the i turn projectile game object to a prefab
and one other thing at first i use gameobject.transform.translare(vector3.forward)
but when turn it to prefab it is not working
but when i use rigidbody.velocity it was moving
dear friend i beg you see my project file that i upload in beginning of page and see its problem
I don't want to create an account there..
but the POINT IS, you are NOT using a prefab for the var
prjectileprefab
OR
you are still trying to access the object in your scene elsewhere after destroying it
if the var IS set to an actual prefab, then it's some other script accessing your original object..
if prjectileprefab is the prefab, then what is the original object in the scene? where is the code for that? that is the object which is being destroyed, which is causing the issue. Where is that code?
In other words the Prefab, and the gameObject in the scene, are TWO different objects, correct?
if you are trying to replace an object being referenced elsewhere, you need to set the object data member to the new instance:
var myProjectile : Transform; // the object in the scene
var myPrefab : Transform; //the prefab
function Update(){
if(myProjectile){//make sure it's not null
//whatever code is trying to access the object in here
}
if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Space)) {
if(!myProjectile) // make sure last one was destroyed
myProjectile = Instantiate(myPrefab, new Vector3(gameObject.transform.position.x, gameObject.transform.position.y, gameObject.transform.position.z), Quaternion.identity); }
}
Answer by robertofantasy · Aug 06, 2012 at 04:53 PM
my friends i Desperately want you download my project(it is just about 2mb) and see its problem and if you can solve it for me thanks from all of you it is in beginning of topic