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 fifty6 · Jul 18, 2012 at 07:40 AM · instantiating

Spawning Enemy

I am trying to spawn an enemy within the vicinity of the player when a trigger is entered. Right now I have the enemy as a prefab. I am using this code to spawn him in

var entity: Transform;

function OnTriggerEnter(collider : Collider) {

if(gameObject.tag=="Spawn") {

Debug.Log("Enemy should spawn"); Instantiate(entity, transform.postion + ( transform.right * 10), Quaternion.identity );

} }

I attach the prefab in the editor to entity

I keep getting an error stating NullReferenceException: Object reference not set to an instance of an object.

Any idea of what I am doing wrong?

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

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by BeHappy · Jul 18, 2012 at 10:26 AM

Try changing,

 if(gameObject.tag =="Spawn")

to

 if(collider.gameObject.tag =="Spawn")

If it doesn't work, please provide with detailed exception message.

Comment
Add comment · Show 3 · 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 fifty6 · Jul 19, 2012 at 04:24 AM 0
Share

This is still not working. I get a NullReferenceException: Object reference not set to an instance of an object Boo.Lang.Runtime.RuntimeServices.InvokeBinaryOperator (System.String, operatorName, System.Object lhs, System.Object rhs)

this is the script now

var entity: Transform;

function OnTriggerEnter(collider : Collider) {

if(collider.gameObject.tag=="Spawn") {

Debug.Log("Enemy should spawn"); Instantiate(entity, transform.postion + (transform.right * 10), Quaternion.identity);

} }

avatar image fifty6 · Jul 19, 2012 at 04:27 AM 0
Share

If I replace the transform.position +(transform.right * 10) with Vector3() it spawns an enemy but I want it to be somewhere near the player. I am pretty sure it is something with that part of the script.

Just to be clear I am using javascript

avatar image Seth-Bergman · Jul 19, 2012 at 05:01 AM 0
Share

in the example, it says "transform.postion", that's the null reference

avatar image
0

Answer by Seth-Bergman · Jul 19, 2012 at 04:47 AM

 function OnTriggerEnter(collider : Collider) {
 
 if(collider.gameObject.tag=="Spawn") {
 
 var nearbyPosition : Vector3 = transform.position;

 var signSpinner = Random.Range(1,10);
 var multiplier = 1;
 if(signSpinner%2 == 1)
 multiplier = -1;
 var posX = Random.Range(5.0,10.0) * multiplier; //or whatever range
 signSpinner = Random.Range(1,10);
 multiplier = 1;
 if(signSpinner%2 == 1)
 multiplier = -1;
 var posY = Random.Range(5.0,10.0)  * multiplier;

 nearbyPosition.x = transform.position.x + posX;
 nearbyPosition.y = transform.position.y + posY;
 
 Debug.Log("Enemy should spawn"); 
 Instantiate(entity, nearbyPosition, Quaternion.identity);
 
 } }

this would randomly pick a nearby spawn location..

EDIT: updated code to include all directions.. as for your code above, you spelled "position" wrong.. transform.postion

Comment
Add comment · Show 6 · 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 fifty6 · Jul 19, 2012 at 06:10 AM 0
Share

I ended up getting it work with this

var position: Vector3 = Vector3(Random.Range(-75, 75), 0, Random.Range(-75, 75));

Instantiate(entity, transform.position +(position), Quaternion.identity);

thanks.

avatar image Seth-Bergman · Jul 19, 2012 at 06:19 AM 0
Share

no prob.. fyi, the only reason for my much lengthier code was to avoid spawning on top of the player.. not likely if the range is this large, but still could occasionally happen.. of course a simple distance check would take care of that, nice streamline of my bulky code!

avatar image fifty6 · Jul 19, 2012 at 06:27 AM 0
Share

Good call i did overlook spawning on top of the player. I'll look into that. Do you have any idea of how I could check the terrain height to insure the enemy always spawns above it.

avatar image fifty6 · Jul 19, 2012 at 06:33 AM 0
Share

I am assu$$anonymous$$g I would use Terrain.activeTerrain.SampleHeight();

avatar image fifty6 · Jul 19, 2012 at 06:41 AM 0
Share

ended up getting it to work. nvm

Show more comments
avatar image
0

Answer by CzechBuh · Jul 31, 2016 at 03:04 PM

Different version for noEnenmy character: https://www.youtube.com/watch?v=L49aHXd9e5o

This script is very simple and fast for your megacity. You can place unlimited character with animations.

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

7 People are following this question.

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

Related Questions

Instantiate objects level with surface 1 Answer

Controlling a specific instance of a GameObject clone or otherwise 2 Answers

Cube rendering glitch, black lines. 0 Answers

Spawning an object on the opposite side of an activator object with no overlaps 1 Answer

Saving in a building game like in "the forest". 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