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
1
Question by robertofantasy · Aug 05, 2012 at 08:43 AM · physicsrigidbodyvelocitybeginners

2d side scrolling problem

hello fellas i want create 2d side scrolling shooter game but i have problem

  1. when i instansiate projectile it is not move

  2. how can make player dont go out of camera

  3. 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

Comment
Add comment · Show 1
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 Fattie · Aug 06, 2012 at 11:44 AM 0
Share

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

5 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by robertofantasy · Aug 05, 2012 at 01:00 PM

please help

Comment
Add comment · 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
0

Answer by robertofantasy · Aug 05, 2012 at 06:22 PM

someone important delete this, I moved it also

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 fafase · Aug 05, 2012 at 06:24 PM 0
Share

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.

avatar image
0

Answer by robertofantasy · Aug 06, 2012 at 11:03 AM

Someone important erase this, I moved it to the question

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 Seth-Bergman · Aug 06, 2012 at 11:33 AM 0
Share

and please don't post comments as answers, just edit your original post

avatar image
0

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

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 robertofantasy · Aug 06, 2012 at 03:38 PM 0
Share

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

avatar image robertofantasy · Aug 06, 2012 at 03:48 PM 0
Share

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

avatar image Seth-Bergman · Aug 06, 2012 at 04:09 PM 0
Share

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); }

}

avatar image robertofantasy · Aug 06, 2012 at 05:55 PM 0
Share

thanks man my problem solved

avatar image
0

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

Comment
Add comment · 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

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

9 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Velocity powered rigidbody on a moving platform without parenting. 3 Answers

Rigidbody character controller can't walk on stairs 0 Answers

Character Joints and Animations results in strange physic animation 1 Answer

Gravitational Object Creation for 3D Game 1 Answer

Calculating force 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