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
0
Question by Conall O · May 18, 2015 at 11:08 AM · c#rigidbodyinstantiate

Adding Force to an instantiated object when it spawns?

Hi, having trouble wrapping my head around achieving this:

I'm making a game where there are 4 spawn points on the right side of the screen.

I have an array of objects(1 in it at the moment) I also have an array of spawnpoints (4)

My aim is to have the object move to the left side of the screen slowly until it reaches a trigger.

My current script is below.

My objects instantiate fine, and spawn in the right spots (grabs a random object from the "Junk" Gameobject[] array, spawns it in one of the 4 spawnpoints from the SpawnPoints[] Transform array.

How do I manipulate the spawned object to move in said motion?

I figure I need to make it a rigidbody, and use rigidbody.addforce, but i'm missing something!

The error message is:

SpawnItems.SpawnJunk() (Line 41): NullReferenceException: Object Reference not set to an instance of an object.

What am I forgetting to pass?

"JunkInstance.AddForce (SpawnPoints[spawnindex].forward * 800);"

Junkinstance is our instanced rigidbody. AddForce demands a vector3, Spawnpoints is our transform array, and spawnindex is a stored transform. .forward returns a vector3, so surely I'm not missing anything? 0_O. This isn't making sense to me :|

 using UnityEngine;
 using System.Collections;
 
 public class SpawnItems : MonoBehaviour {
 
 
     // indexes Junk into an array
     // indexes SpawnPoints into an array
     // Spawns random Junk obj from a random SpawnPoint.
     // Assign object to move down the lane it spawned in.
 
     public Transform[] SpawnPoints; // array of spawnpoints, linked with transform in editor. this is also beginning of our "lanes".
     public float SpawnTime = 1.5f; // time between spawn "waves"
     public GameObject[] Junk; // Array of Junk Objects
 
     // Use this for initialization
     void Start () {
     
         InvokeRepeating ("SpawnJunk", SpawnTime, SpawnTime);
         // invokes SpawnJunk method after $SpawnTime seconds, then again every $SpawnTime seconds.
 
     }
     
     // Update is called once per frame
     void Update () {
     
     }
 
     void SpawnJunk()
     {
         int spawnindex = Random.Range (0, SpawnPoints.Length);
             // set index number of the array randomly
 
         int objectIndex = Random.Range (0, Junk.Length);
             // object Index = random number in range of between 0 & GameObject[] Junk.Length
 
         Rigidbody JunkInstance;
         JunkInstance = Instantiate (Junk[objectIndex], SpawnPoints [spawnindex].position, SpawnPoints [spawnindex].rotation) as Rigidbody;
         // Instantiates a random Junk obj to a random spawnpoint, following the spawnpoint's rotation.
 
         JunkInstance.AddForce (SpawnPoints[spawnindex].forward * 800);
         // BROKEN!
     }
 }
 
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 Conall O · May 19, 2015 at 09:02 AM 0
Share

thank you both, its much appreciated!

2 Replies

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

Answer by Cherno · May 18, 2015 at 11:36 AM

 JunkInstance.AddForce (SpawnPoints[spawnindex].forward * 800);

needs to be

 JunkInstance.GetComponent<RigidBody>().AddForce (SpawnPoints[spawnindex].forward * 800);


As you try to use the AddForce function with a GameObject, but you have to use it with a rigidbody that is on a gameobject.

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 Conall O · May 18, 2015 at 07:04 PM 0
Share

kind of thought the engine would check for an attached rigidbody on said gameobject, but hey , now I know :)

avatar image
1
Best Answer

Answer by HTwist · May 18, 2015 at 12:12 PM

The result of the Instantiate method is a GameObject because the object given to it is a GameObject. Casting it with a "as" will be null.

So JunkInstance will always be null.

Either use a array of rigid body or cast to GameObject then get the rigid body component.

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 Cherno · May 18, 2015 at 12:25 PM 0
Share

^HTwist is right, I didn't even notice that. Instantiate as a gameobject and then use the corrected line from my answer and all will be fine :)

 JunkInstance = Instantiate (Junk[objectIndex], SpawnPoints [spawnindex].position, SpawnPoints [spawnindex].rotation) as GameObject;

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

Instantiate rigidbody C# 1 Answer

Distribute terrain in zones 3 Answers

Multiple Cars not working 1 Answer

OnCollisionEnter not being called, between a Rigidbody and a Box Collider 1 Answer

Instantiated prefab doesn't collide with player 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