• 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 Polatrite · Oct 19, 2010 at 06:49 PM · instantiateprefab

Creating objects and ordering them to act

For some reason I'm having substantial problems simply instantiating a GameObject of a certain type, and then making it perform actions. I think the problem is that I'm missing some kind of underlying fundamentals on how Unity behaves.

I have a GUI button, when clicked it calls the ButtonPressed_Green() function on the target, the target being an instance of the character in game.

function ButtonPressed_Green() { var blob = Instantiate(blobFab, Vector3(transform.position.x, transform.position.y+5, transform.position.z), Quaternion.identity); blob.movement.direction.x = 1; blob.SendMessage("ToggleWalking");

}

This code should create an instance of blobFab, a variable on my script that is set to the prefab BlobParent. It does that part fine, but the second line "blob.movement.direction.x" does not work.

Movement is a variable of the BlobController script (this same script, this blob is essentially duplicating itself) that refers to the BlobMovement class with some variables for x/y/z. How do I access these variables using the GameObject reference, blob, itself?

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 Polatrite · Oct 19, 2010 at 06:56 PM

As always, I find the answer 5 minutes after asking, despite tinkering for ages beforehand!

I must use GetComponent() to retrieve the actual script object, then reference those variables through the script object.

function ButtonPressed_Green() { var blob = Instantiate(blobFab, Vector3(transform.position.x, transform.position.y+5, transform.position.z), Quaternion.identity); var blobmovement : BlobController; blobmovement = blob.GetComponent("BlobController"); blobmovement.movement.direction.x = 1; blob.SendMessage("ToggleWalking");

}

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 Bravini · Oct 20, 2010 at 01:17 AM

or you could make it into a list of blobs as the new blob will receive the name (Clone) after it, and listing would make you able to synchronize, halt or find em easier.

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

when prefabs are spawned I want to touch them to destroy. help? 0 Answers

How to get unique reference to instantiated prefab 1 Answer

Clone a Prefab 1 Answer

Modifying scene objects programatically references prefab 1 Answer

The prefab you want to instantiate is null. 2 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges