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 Rick74 · Aug 25, 2013 at 12:17 PM · javascriptinstantiatevariable

How do I assign a var to the object I've just instantiated?

The object I instantiate has a var: GameObject that I would like to assign an object too. But I'm not sure how this works in Java script.

The code I have so far;

 if ( Input.GetMouseButtonDown (0) && lastHitObj )
            {
                if ( lastHitObj.tag == "placementPlane_Open" )
                {
                    if ( structureIndex == 0 )
                    {    
                        var ObjectOffset : Vector3 = new Vector3( 0.1f, 0.5f, 0);
                        Instantiate(allStructures[structureIndex], lastHitObj.transform.position + ObjectOffset, Quaternion.identity );                    
                    }

                lastHitObj.renderer.material = originalMat;
              hudOn = false;
              bigUnit = false;
                }

So immediatly after I've instantiated the object, I wanna say something like; InstantiatedObject.variable = lastHitObj

I hope this makes sense!

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
1
Best Answer

Answer by KiraSensei · Aug 25, 2013 at 12:48 PM

The first thing to do is to get the instantiated object in a variable :

 var myInstantiatedObject:GameObject = Instantiate(allStructures[structureIndex], lastHitObj.transform.position + ObjectOffset, Quaternion.identity );

I assume here that allStructures[structureIndex] is a GameObject, so the instantiated object is a GameObject too... See the doc here.

Then, to valuate a variable "in" the game object, you need to attach a script to it first. You have several way : first you can have one already in the prefab used ti instantiate the game object (so allStructures[structureIndex] needs to have a script), or you can create your script on runtime with AddComponent. This method returns the added component, you will need it after, so keep it in a variable :

 var myObjScript:MyScript = myInstantiatedObject.AddComponent(MyScript);

In this script, you need a variable (I assume of type "GameObject" again) that will contain lastHitObj. I assume you already know how to do that ...

Finally, in the script you put in your question you will be able to do :

 myObjScript.variable = lastHitObj;

You cannot put variables directly on a game object, only in scripts attached to a game object.

Comment
Add comment · Show 10 · 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 Rick74 · Aug 25, 2013 at 02:29 PM 0
Share

If I have to place allot of these gameobjects down, should I clear out the variable? If I clear out the variable, will that delete the game object?

The goal here is to instantiate an object and have it store in it's script the last hit object my mouse was over. ("lastHitObj")

I'm going to be placing allot of these structures down and each one needs to store whatever the last hit object was when I instantiated it.

avatar image KiraSensei · Aug 25, 2013 at 02:34 PM 0
Share

But do you really need game objects ?

$$anonymous$$aybe you can do what ever you want to do just with one script attached to the player that will have variables with the objects the mouse hits ?

What do you really want to do ? If you only need to stock a list of game objects hit by the mouse, you should have one script attached to the player, you don't need other game objects.

avatar image fafase · Aug 25, 2013 at 02:34 PM 1
Share

Basic program$$anonymous$$g books would tell you the principle of scope.

When declaring between two {} the variable is destroyed when reaching the }

 if ( something ) {
       if ( somethingElse ){    
            // Create obj
            var obj = Instantiate(allStructures[structureIndex], lastHitObj.transform.position + ObjectOffset, Quaternion.identity );           
       } <-obj is destroyed
      obj.member = value; <-error, obj does not exist in this scope
 }

What I mean is that you can create thousands of objects inside the if statement, the reference won't make it out of the }.

avatar image Rick74 · Aug 25, 2013 at 02:46 PM 0
Share

It's a tower defense type situation;

I have a grid of sprites on the ground, side by side. Each sprite, it's own object of course. The sprite objects have the tag "open". If I mouse click over a sprite with the tag "open" i can drop a gameobject on it. But now I need the sprite to be tagged "closed" so that I no longer can drop units on that same sprite.

If the gameobject stores the sprite it's on as a variable, it can tell it to tag itself as "open" before that gameobject gets destroyed.

I could achieve this through functions like OnTriggerStay, but because their are so many sprites and the potential of a ton of game units, I'm trying to avoid using a call that happens every frame, such as "OnTriggerStay".

thanks for clearing that up fafase!

avatar image Rick74 · Aug 25, 2013 at 04:38 PM 0
Share

Hrm, still having trouble with this! Here's where I'm at;

 if ( Input.Get$$anonymous$$ouseButtonDown (0) && lastHitObj )
            {
                if ( lastHitObj.tag == "placementPlane_Open" )
                {
                    if ( structureIndex == 0 )
                    {    
                        var myObjectOffset : Vector3 = new Vector3( 0.1f, 0.5f, 0);
                        var myObject : GameObject = Instantiate(allStructures[structureIndex], lastHitObj.transform.position + myObjectOffset, Quaternion.identity );
                        myObject.tilePartner = lastHitObj;                    
                    }
                    
                lastHitObj.tag = "placementPlane_Closed";
                lastHitObj.renderer.material = original$$anonymous$$at;
                }
            }

this gives me the error;

BCE0019: 'tilePartner' is not a member of 'UnityEngine.GameObject'.

"tilePartner" is a GameObject variable inside of the script component attach to "myObject".

What am I missing here?

Show more comments
avatar image
0

Answer by Rick74 · Aug 26, 2013 at 06:18 AM

So after a bit more research online, and some thumbing through old tutorial codes I found the answer! (which might have already been said), but I'll just type it out so theirs no confusion;

this line;

myObject.tilePartner = lastHitObj;

needed to be changed to this;

myObject.GetComponent(myObjectScript).tilePartner = lastHitObj;

sigh...works perfectly now.

Thanks everyone for helping me to get there!

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

16 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

Related Questions

Putting instantiate inside a variable 2 Answers

[Urgent]Changing variables of an object AFTER instantiation 2 Answers

Instantiate JS error that i can't figure out 1 Answer

Changing variable in a script of a game object hit with raycast 1 Answer

Setting a variable to an instantiated object. 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