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 No_Username_Found · Apr 19, 2014 at 12:58 AM · gameobjecttransformmethod

Access method of an instance

I have an instance of a quad that has a method SetTexture(). I can instantiate the quad, but it has to be a transform so that I can set it as a child. But I still need to be able to access quad.SetTexture() so that I don't have to have a different prefab for every texture. Commented lines don't work.

 //squarePrefab.SetTexture();
                         //squarePrefab.transform.SetTexture();
                         //squarePrefab.gameObject.SetTexture();
                         //squarePrefab.transform.gameObject.SetTexture();
                         Transform gridPart = (Transform) Instantiate(squarePrefab, pos, Quaternion.identity);
                         gridPart.transform.parent= transform;
                         //gridPart.gameObject.SetTexture();
                         //gridPart.SetTexture();
                         //(GameObject) gridPart.SetTexture();
                         //Square gridPart = Square("Texture/Terrain/dirt0");
                         //Transform instance = (Transform) Instantiate(gridPart);
                         //GameObject gridPart = (GameObject) Instantiate(squarePrefab, pos, Quaternion.identity);
                         //gridPart.SetTexture();
Comment
Add comment · Show 1
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 getyour411 · Apr 19, 2014 at 01:00 AM 0
Share

You can instantiate the quad as a GameObject and use gridPart.gameObject.transform; I didn't get the rest, just pointing out you don't have to use Transform just to access parent/child

1 Reply

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

Answer by NoseKills · Apr 19, 2014 at 01:28 AM

In order to understand what's happening and how to fix this, you need to understand the way the building blocks of unity games are constructed.

A GameObject or a Transform does not have a function called SetTexture(). A GameObject can have a component attached to it and this component can have a method called SetTexture().

How do you know there is a method called SetTexture()? If you added a script to the prefab and the script has that method, you should do:

 GameObject gridPart  = (GameObject)GameObject.Instantiate(squarePrefab);
         gridPart.transform.parent = transform;
         
         YourScriptClassName script = gridPart.GetComponent<YourScriptClassName>();
         script.SetTexture();

But if you're for example talking about the SetTexture method of the Material class, you should:

 GameObject gridPart  = (GameObject)GameObject.Instantiate(squarePrefab);
         gridPart.transform.parent = transform;
 
         MeshRenderer mr = gridPart .GetComponent<MeshRenderer>();
         mr.material.SetTexture("_MainTex", yourTextureVariable);
 


You can mix and match but you have to stay true to Types in your code:

 using UnityEngine;
 using System.Collections;
 
 public class Test : MonoBehaviour {
 
     public GameObject squarePrefabGameObject;
     public GameObject gridPartGameObject; 
 
     public Transform squarePrefabTransform;
     public Transform gridPartTransform; 
 
     void functionX () 
     {
         //Option 1
         // If the prefabs are declared as GameObjects, you have to stay true to the type...
         gridPartGameObject  = (GameObject)GameObject.Instantiate(squarePrefabGameObject);
         // you can still access the transfrom easily...
         gridPartGameObject.transform.parent = transform;
 
         MeshRenderer mr = gridPartGameObject.GetComponent<MeshRenderer>();
         mr.material.SetTexture("_MainTex", _Texture);
 
         // if you added a script with yur own functions to the prefab, you need to get the script first...
         YourScriptClassName script = gridPartGameObject.GetComponent<YourScriptClassName>();
         script.SetTexture();
     }
 
     void functionY()
     {
         // Option 2
         // If the prefabs are declared as Transforms...
         gridPartTransform  = (Transform)GameObject.Instantiate(squarePrefabTransform);
         gridPartTransform.parent = transform;
         
         MeshRenderer mr = gridPartTransform.GetComponent<MeshRenderer>();
         mr.material.SetTexture("_MainTex", _Texture);
         
         YourScriptClassName script = gridPartTransform.GetComponent<YourScriptClassName>();
         script.SetTexture();
 
 
 
         // a "transform" has a reference to "gameObject" and vice versa, so whichever your prefabs are declared as
         // you can access one via the other
         GameObject instantiatedGameObject = (GameObject)GameObject.Instantiate(squarePrefabGameObject);
 
         // a GameObject has a transform that describes its orientation and position 
         gridPartTransform = instantiatedGameObject.transform;
 
         // a transform is connected to a GameObject
         gridPartGameObject = gridPartTransform.gameObject;
     }
 
 
 }
 
Comment
Add comment · Show 3 · 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 getyour411 · Apr 19, 2014 at 01:48 AM 0
Share

Hope you don't $$anonymous$$d, I converted your comment to an Answer.

avatar image No_Username_Found · Apr 19, 2014 at 02:38 AM 0
Share

Well this isn't quite doing it for me, though it answers the question in part. The object has to be a transform because it can't be instantiated as a gameobject (for some reason).

"Can't cast from source type to destination type"

I should have mentioned that originally.

And I wrote the SetTexture() method as a way of shortening the code.

avatar image NoseKills · Apr 19, 2014 at 09:18 AM 0
Share

I guess the "squarePrefab" is of type Transform then? I'll change my answer a bit.

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

21 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

Related Questions

Adding a prefab to gameObject at a certain position in runtime 0 Answers

Creating new Transform from existing objects Transform to Instantiate object 1 Answer

Animation issue (OnTriggerEnter) ***SOLVED*** 1 Answer

move a GameObject based on Input.GetTouch 1 Answer

Scale (Transform.localscale) a gameobject based on ARFaceAnchor anchorData 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