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 HumanError · Dec 14, 2012 at 05:21 PM · errornullreferenceexceptiontagspawningsendmessage

Remmember the spawner

I asked this question before and I didn't get any answer or a tip for some days. I can't continue building the game without it:

I made some helicopters which spawns whenever another helicopter is destroyed. The helicopter follow me from above and when its run out of ammo it should fly back exactly to where it spawned. For this, I put a tag on some objects which can spawn them.

the problem is that when the helicopter spawns it can't choose the object that spawned it because I have many of them. The code works fine when I have only one object that can spawn helicopters.

After I got an answer there I got these two codes:

on the spawner:

 var chopper : Transform;
 var spawnPoint : Transform;
 var spawningEffect : Transform;
 var isAlive = false;
 
 function Update ()
 {
    if(!isAlive)
    {
       Spawn ();
    }
 }
 
 function Spawn ()
 {
    isAlive = true;
    var effect = Instantiate (spawningEffect, spawnPoint.transform.position, Quaternion.identity);
    yield WaitForSeconds (4.0);
    var spawnChopper = Instantiate (chopper, spawnPoint.transform.position, Quaternion.identity);
    spawnChopper.SetSpawner(this);
 }

On the helicoper:

 var landingPlate;
 
 function SetSpawner (spawner : GameObject)
 {
    landingPlate = spawner;
 }
 

I'm getting this error: NullReferenceException: Object reference not set to an instance of an object.

What is the problem?

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by MibZ · Dec 14, 2012 at 05:34 PM

The "this" keyword references the script it is written inside of, not the GameObject.

 //Change the code in spawner to this
 spawnChopper.SendMessage ("SetSpawner", this.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 HumanError · Dec 15, 2012 at 07:40 AM 0
Share

Now I get this error: $$anonymous$$issingFieldException: Field 'UnityEngine.Vector3.position' not found.

avatar image
0

Answer by Bunny83 · Dec 15, 2012 at 10:13 AM

You included way too less information. We don't know how you spawn the chopper, also we don't know what spawnChopper is actually referencing, the spawner, or the spawnedChopper?

I would do something like this:

 //Chopper.js
 public var mySpawner : Spawner;
 
 function Update()
 {
     // chopper stuff
     if (ShouldFlyBack)
         FlyTo(mySpawner.transform.position);
 }
 
 //Spawner.js
 
 public var chopperPrefab : Chopper;  // assign in the inspector
 
 function Spawn()
 {
     var clone : Chopper = Instantiate(chopperPrefab, transform.position, transform.quaternion);
     clone.mySpawner = this;
 }

Now you can always reference back to the spawner script (and it's gameobject / transform) whenever you want by using the mySpawner reference in the chopper.

If you don't like this as public variable, you can make it private and create a setter:

 //Chopper.js
 private var mySpawner : Spawner;
 
 function SetSpawner(aSpawner : Spawner)
 {
     mySpawner = aSpawner;
 }
 
 function Update()
 {
     // chopper stuff
 }
 
 //Spawner.js
 
 public var chopperPrefab : Chopper;  // assign in the inspector
 
 function Spawn()
 {
     var clone : Chopper = Instantiate(chopperPrefab, transform.position, transform.quaternion);
     clone.SetSpawner(this);
 }


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 HumanError · Dec 15, 2012 at 01:52 PM 0
Share

spawnChopper is the Chopper and landingPlate is the spawner. I will put more information if it not enough.

Both codes you gave me don't work, I get an error that there are no such types of varibles Spawner and Chopper: The name 'Spawner' does not denote a valid type ('not found'). Did you mean 'System.Runtime.Remoting.$$anonymous$$etadata.W3cXsd2001.SoapAnyUri'?

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

11 People are following this question.

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

Related Questions

Editing in Animator removes Player tag 0 Answers

[js]NullReferenceException: Object reference not set to an instance of an object 1 Answer

Console throwing errors when trying to rename or highlight elements. 1 Answer

Hi I get an error code when i'm using my Javascript and I can't understand how to fix it... :( 1 Answer

What is a Null Reference Exception In Unity. 4 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