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 akekajoo · Dec 02, 2011 at 11:04 AM · instantiateprefab

Manipulating a prefab from within the script that instantiated it

I'm new to Unity coming from XNA, and I feel like I'm seriously missing something.

what I would like to do is have a GO instantiate a prefab, and then have access to that instance's public fields and methods (rather, the fields and methods of the script attached to the prefab). For example, if I wanted to have a "GameBoard" object instantiate a "Tile" prefab in a 3D version of a board game, and then based on some condition within the GameBoard, tell the Tile to call one of its methods. I haven't seen a way to do this on any tutorials, and I haven't seen it asked on this forum - what am I missing?

Thanks!

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

3 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by save · Dec 02, 2011 at 11:20 AM

There are a couple of ways of doing it, depends on what pipeline you're after. It's always easier to have an object that functions as a manager for a level or a complete game.

Declaring via static components:

var gameBoardPrefab : GameObject;

function Start () { Instantiate(gameBoardPrefab, Vector3.zero, Quaternion.identity); }

function DoSomething () { gameBoardScript.foo = "bar"; //Set foo : String in gameBoardScript to "bar" gameBoardScript.foo(); //Run function foo() in gameBoardScript }

Declaring via cached components:

var gameBoardPrefab : GameObject; static var gameBoard : GameObject; static var gBcomponent : gameBoardScript;

function Start () { gameBoard = Instantiate(gameBoardPrefab, Vector3.zero, Quaternion.identity); gBcomponent = gameBoard.GetComponent(gameBoardScript); }

function DoSomething () { gameBoard.gBcomponent.foo = "bar"; //Set foo : String in gameBoardScript to "bar" gameBoard.gBcomponent.foo(); //Run function foo() in gameBoardScript }

The same method would be working for the game board and its tiles where an array would come in handy.

tile[64].SomeFunction(SomeValue);
Comment
Add comment · Show 2 · 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 aldonaletto · Dec 02, 2011 at 12:25 PM 1
Share

@akekajoo, in the second example gameBoardScript is the type of the script. This type is the script name without quotes or extension: if this script is called "gameBoardScript.js", its type will be gameBoardScript.

avatar image save · Dec 09, 2011 at 10:06 PM 0
Share

Thanks @aldonaletto for filling in!

avatar image
0

Answer by akekajoo · Dec 02, 2011 at 08:45 PM

Thanks for the response, though I can't seem to get this method (in particular the 2nd one) to work using C#. Here's what I did as a test:

I have GameBoardPrefab and TilePrefab, and they have attached to them GameBoardScript.cs and TileScript.cs. All TileScript has is the following (Where can I find instructions for formatting code as you did in your response, btw?):

string foo;

public void foo(Vector3 moveVector)

 {
     gameObject.transform.Translate(moveVector);
 }

GameBoardScript has the following, which was my attempt to translate your JS example to C#:

GameObject tilePrefab;

static GameObject tile;

static TileScript tileScript;

void Start() {

     tile = (GameObject)Instantiate(tilePrefab, new Vector3(5, -2, 0), Quaternion.identity);
     tileScript = tile.GetComponent<TileScript>();

Visual Studio didn't like the code until I cast the instantiation as a GO, but there we are, I guess.

At this point, I tried to add "tile.tileScript.foo = "bar";" but this caused the following error (and what is the point of this foo = "bar" business anyway, if I may ask?):

Error 1 'UnityEngine.GameObject' does not contain a definition for 'tileScript' and no extension method 'tileScript' accepting a first argument of type 'UnityEngine.GameObject' could be found (are you missing a using directive or an assembly reference?)

Adding the line for calling foo() caused a duplicate error.

Thank you again!

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 akekajoo · Dec 03, 2011 at 11:20 PM

For closure, I figured it out, and the only problem was that I had somehow unassigned the prefab. After loading the prefab in code from a resources folder, it's all good. And embarrassingly, I didn't realize that setting the string foo to "bar" was just an example of doing something with the prefab's script, though I understood that with the foo method example.

Anyway, thanks a lot!

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Instantiated Prefab's script doesn't work 2 Answers

How to access script variables attached to a prefab at runtime in Javascript? 2 Answers

Calling a function from a instantiated clone 1 Answer

Instantiating Prefab from Javascript - BCE0005: Unknown identifier: 'Prefab' 2 Answers

[Solved]Instantiating prefab from a script and destroy it from another one 2 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