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 OgityBogityBoo · Dec 16, 2015 at 04:28 AM · 2dgameobjectinstantiatevariableassign

Assign GameObject OnMouseUp not through inspector -2D

So I have a GameObject named "selectedUnit", which can be moved across tiles via clicking. It works great when I drag a unit/object into the "selectedUnit" bit but that would mean that object will be forever selected.

Where as, realistically, there will be dozens of Units on screen, all with their own movement abilities and all of which can be controlled by the player. So the selectedUnit Object needs to be variable.

I've tried a dozen of tactics that I can think of and or find by researching online. Nothing seems to be able to assign the GameObject.

Currently I am instantiating the unit using this code

 void GenerateUnitVisual(){
     UnitType ut = unitTypes [tiles [1, 1]];
     GameObject go = (GameObject)Instantiate (ut.unitVisualPrefab, new Vector3 (1, 1, 0f), Quaternion.identity);
     Unit u = go.GetComponent<Unit> ();
     u.unitName = ut.name;
 }

Which is hard coding one unit to spawn but I will get to not hard coding it later once I get this working.

The UnitType instance, in this case "ut", has two variables within in, name, and unitVisualPrefab, which are self explanatory.

The Unit class contains variables for x and y, for positions and UnitName, for the string name.

I've tried something along the lines of

 selectedUnit.GetComponent<Unit>().unitName = GameObject.Find(unitName);

But that won't work since the unitName is literally the variable name I made in UnitTypes.

I was wondering if something like

 u.unitID = this;

But I always get some sort of error about can't implicitly convert type to unit.

Regardless, I'm at a loss. Surely this should be simple. like OnMouseUp(){selectedObject = this;} or something.

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Knnthra · Dec 16, 2015 at 07:46 AM

This can be done in a lot of different way.

Inside your main script (I will call it GameManger from now on), that allows your unit to move you can make a static variable, that works as the target or make your GameManager into a singleton, as I did in my example:

 public class GameManager : MonoBehaviour
     {
     private static GameManager instance;
 
         public static GameManager Instance
         {
             get
             {
                 if (instance == null)
                 {
                     instance = GameObject.FindObjectOfType<GameManager>();
                 }
                 return instance;
             }
         }
     
         public Unit Target { get; set; }
     }

On your UnitScript you can add the following:

 private void OnMouseUp()
 {
     GameManager.Instance.Target = this;
 }


Personally I prefer to use a singleton over a static variable, but you can also just make a public static variable if you want to.

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

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

Related Questions

Instantiate a prefab and assign it's values 1 Answer

[solved] A variable is being set by a variable from a different instance of the script when it should not be. 0 Answers

Instantiating object randomly withing the boundaries of an object. 0 Answers

Game Object doesn't instantiate at Mouse Position 3 Answers

instantiate prefab at every gameobject with tag 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