Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by BlackVandal · Jan 28, 2016 at 09:36 PM · javascriptgameobjecttransformtarget

Null Reference Exception for target in a script on a newly instantiated object

What I'm trying to do is spawn an enemy and create a marker/blip on the Minimap for the spawned enemy. I get a NullReferencException at line BlipCtrl:29.

The Blips on the Minimap have a Target GameObject which position reflects the position of the Blip on the Minimap. When an Enemy is instantiated I set it to have a unique name. I'm having trouble writing a function the sets the Target of the Blip to the GameObject with the Unique name that the Blip gets it's position and rotation from.

Spawner.js

 public var enemy : Rigidbody ;
 private var spawnValues : Vector3 = Vector3(50, 1, 50);
 public var enemycount : int ;
 public var enemyId : int;
 static var enemiesarray = new Array();
 public var blipcontroller : BlipCtrl; 
 
 var spawnPosition : Vector3;
 var spawnRotation : Quaternion;
 
 
 function Start () {
 var blipcontroller = GetComponent("BlipCtrl");
     SpawnWaves();
     
     
 }
 
 function SetName () {
     enemy.name = "Enemy#" + enemyId++;
 }
 
 function SpawnWaves() {
     for(var i = 0; i < enemycount; i++) {
         var spawnPosition : Vector3 = new Vector3(Random.Range(-50, 50), spawnValues.y ,Random.Range (-50, 50));
         var spawnRotation : Quaternion = Quaternion.identity;
         SetName();
         Instantiate(enemy, spawnPosition, spawnRotation);
         enemiesarray.Push(this.enemy);
         blipcontroller.CreateBlip(enemy, spawnPosition, spawnRotation);
     }
 }


BlipCtrl.js

 public var EnemyBlipImage : Rigidbody;
 public var bliparray = new Array();
 public var blipID : int;
 public var map : Minimap;
 private var blipTarget : Blip;
  private var gameobject : GameObject;
  static var target : Transform;
  var newBlip : GameObject;
  
 function Awake () {
 blipTarget = GetComponent(Blip);
 map = GetComponent(Minimap);
 
 }
 
 function SetName() {
 EnemyBlipImage.name = "EnemyBlip#" + blipID++;
 
 }
 
 
 function CreateBlip(object, objectName, position , rotation) {
 SetName();
 var newBlip = Instantiate(EnemyBlipImage, position, rotation);
 newBlip.transform.SetParent(map.transform);
 newBlip.gameObject.tag = "Enemy" ;
 gameobject = GameObject.Find(objectName + "(Clone)");
 blipTarget.target = gameobject.transform;
 blipTarget.SetTarget(gameobject);
 }

Blip.js

    #pragma strict
  public var target : Transform ;
  public var keepInBounds = true;
  public var LockScale = false;
  public var LockRotation = false;
  private var map : Minimap ;
  private var myRectTransform : RectTransform ;
  
  //set target to gameobject
 function SetTarget(gameobject) {
  target = gameobject;
 }
 
 function Start () {
      map = GetComponentInParent(Minimap);
     myRectTransform = GetComponent(RectTransform);
 
 }
 
 function Update () {
 
 }
 
 function LateUpdate() {
      var newPosition : Vector3 = map.TransformPosition(target.position);
      
      if(keepInBounds) {
      newPosition = map.MoveInside(newPosition);
      }
      
      if(!LockScale) {
      myRectTransform.localScale = new Vector3(map.zoomLevel, map.zoomLevel, 1);             
      }
      if(!LockRotation) {
      myRectTransform.localEulerAngles = map.TransformRotation(target.eulerAngles);
      }
      
      myRectTransform.localPosition = newPosition;
          
 }




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

0 Replies

· Add your reply
  • Sort: 

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

52 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

[Urgent] Finding Nearest Target 2 Answers

How do you convert UnityEngine.GameObject to UnityEngine.Transform? 0 Answers

How do I find a player for AI to target when spawning a player in? 0 Answers

Assets/CArSmoothFollow.cs(40,68): error CS0246: The type or namespace name `ExampleGame_Game' could not be found. 1 Answer

Change flashlight color when object is picked up, 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