• 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 Laurgrin · Feb 19, 2016 at 04:44 PM · instantiateprefab-instance

Prefabs not working.

Basically, the prefab instances of the object do nothing, even with all the same scripts linked to them. If fill the required links with objects from the hierarchy, it works. If the required links are from other prefabs it doesn't work. At all. Even if those prefabs are made by dragging said objects into the Project tree. I'm completely lost why.

Edit: UnassignedReferenceException: The variable selection of PlayerMoveUnit has not been assigned. You probably need to assign the selection variable of the PlayerMoveUnit script in the inspector. UnityEngine.GameObject.GetComponent[DetectSelection] () (at C:/buildslave/unity/build/artifacts/generated/common/runtime/UnityEngineGameObjectBindings.gen.cs:38) PlayerMoveUnit.Update () (at Assets/Scripts/PlayerMoveUnit.cs:48)

It seems something fucky goes with references when you reference other prefabs. Here's the script in question:

 using UnityEngine;
 using System.Collections;
 using UnityEngine.UI;
 
 public class PlayerMoveUnit : MonoBehaviour {
 
     public Text moveCostText;
     public GameObject selection, moveUI;
 
     public Sprite spriteSelected;
     public Sprite spriteNotSelected;
 
     private bool isSelected;
 
     private int id, actionPoints, sid;
 
     //===================================================================================
     //Initialization, obviously.
     //===================================================================================
     void Start ()     {
         isSelected = false;
         RefreshActionPoints ();
         sid = gameObject.GetInstanceID ();
     }
     //===================================================================================
     //Activate object when clicked on.
     //===================================================================================
     void OnMouseDown()     {    
         if (id == sid) {
             isSelected = true;
         } 
     }
     //===================================================================================
     //Variable setup and the moving itself is here.
     //===================================================================================
     void Update ()    {
         //variable setup
         Vector3 mousePosition = Camera.main.ScreenToWorldPoint (new Vector3 (Input.mousePosition.x, Input.mousePosition.y, 0));
         Vector3 targetPosition = new Vector3 (Mathf.Floor (mousePosition.x), Mathf.Floor (mousePosition.y), 0);
         int moveCost = MoveCost (transform.position.x, transform.position.y, mousePosition.x, mousePosition.y);
 
         //deselect the object if other is selected
         if (Input.GetMouseButtonDown (0) && (isSelected) && (id != sid ) ) {
             DeactivateSelection ();
         }
 
         //get the id of the object under the mouse.
         id = selection.GetComponent<DetectSelection>().SelectedUnit;
 
         //Change sprite to spriteSelected to show the player which unit is selected.
         if (isSelected) {
             GetComponent<SpriteRenderer> ().sprite = spriteSelected;
         } else {
             GetComponent<SpriteRenderer> ().sprite = spriteNotSelected;
         }
 
         //Move to mouse position.
         if ((Input.GetMouseButtonDown(1) ) && (isSelected) && (id == 0)
             && (mousePosition.x >= 0.0) && (mousePosition.x <= 6.0) && (mousePosition.y >= 0.0) && (mousePosition.y <= 9.0)
             && (actionPoints - moveCost >= 0))          {
                 transform.position = targetPosition;
                 actionPoints -= moveCost;
                 DeactivateSelection ();
         }
             
         //Move AP cost indicator.
         if (((mousePosition.x >= 0.0) && (mousePosition.x <= 6.0) && (mousePosition.y >= 0.0) && (mousePosition.y <= 9.0))
             && ((isSelected) && (selection.GetComponent<DetectSelection>().UnitTag == "None") && (targetPosition != transform.position)) ) {
             print (selection.GetComponent<DetectSelection>().UnitTag);
             moveUI.transform.position = targetPosition;
             moveCostText.text = moveCost + " AP";
         }
     }
     //===================================================================================
     //Calculate move cost.
     //===================================================================================
     int MoveCost (float currentX, float currentY, float targetX, float targetY) {
         float xMove, yMove;
         xMove = Mathf.Abs(Mathf.Floor(currentX) - Mathf.Floor(targetX));
         yMove = Mathf.Abs(Mathf.Floor(currentY) - Mathf.Floor(targetY));
         int moveCost = (int) (xMove + yMove);
         return moveCost;
     }
     //===================================================================================
     //Refresh all actions points. Should be called at the start of the player's turn.
     //===================================================================================
     void RefreshActionPoints (){
         actionPoints = GetComponent<PlayerStats>().actionPoints;
     }
     //===================================================================================
     //Disable and move away the move ap cost counter.
     //===================================================================================
     void DeactivateSelection (){
         moveUI.transform.position = new Vector3 (99.0f, 0.0f, 0.0f);
         //moveUI.SetActive (false);
         isSelected = false;
     }
     //===================================================================================
     //Check against unit id. 
     //===================================================================================
 
     //===================================================================================
     //End of the script.
     //===================================================================================
 }
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
Best Answer

Answer by Laurgrin · Feb 22, 2016 at 05:21 PM

For some reason it worked when I referenced other objects in Start(). For some reason it refused to acknowledge the objects referenced via the inspector in the editor.

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 ewaneverytime · Feb 19, 2016 at 05:11 PM

What is the Component "DetectSelection" you are trying to get?

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 Laurgrin · Feb 21, 2016 at 10:00 AM 0
Share
 using UnityEngine;
 using System.Collections;
 
 public class DetectSelection : $$anonymous$$onoBehaviour {
 
     public GameObject gamObject;
 
     private int selectedUnit;
     private string unitTag;
 
     //this works wtf
     public int SelectedUnit    {
         get {
             return selectedUnit;
         } set {
             selectedUnit = value;
         }
     }
 
     public string UnitTag {
         get {
             return unitTag;
         } set {
             unitTag = value;
         }
     }
 
     void Start () {
         Instantiate (gamObject, new Vector3 (0, 0, 0), Quaternion.identity);
     }
 
     // Update is called once per frame
     void Update ()     {
         Vector3 pos = Camera.main.ScreenToWorldPoint (Input.mousePosition);
         RaycastHit2D hit = Physics2D.Raycast(pos, Vector2.zero);
         if (hit.collider != null) {
             selectedUnit = hit.collider.gameObject.GetInstanceID();
             unitTag = hit.collider.gameObject.tag;
         } else {
             selectedUnit = 0;
             unitTag = "None";
         }
                 
     }
 }
 

It is attached to the camera for various detection purposes. $$anonymous$$ainly what the player is mousing over.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Instantiate more than 1 prefab 0 Answers

How to initiate a prefab with correct Y position on a terrain 3 Answers

The prefabs stop instantiating after some time. 1 Answer

[Solved] Destroy instantiated prefabs with a same tag, one by one, from last instantiated to first ? 2 Answers

How to set created variables on an instantiated prefab different from variables such as Rigid body and Render? 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