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 GabeOfThrones · Dec 20, 2012 at 06:39 AM · c#instance

Can't access the script of an instantiated prefab :(

I have this code that works fine:

 public class Main : MonoBehaviour {
     
     public Human prefab;
     
     void Start(){        

         Human human = (Human)Instantiate (prefab);    

         }
 }

It creates a human that walks around and does what he should. What's weird is that "Human" is the name of the class, not the prefab. What I'd like to do is alter some properties in this script. But if I try:

 human.speed = 5;

...I get a CS1061 error about it not containing definitions. I've looked this up on the forums and in the manual, and there seems to be clear answers, but when I implement them it doesn't work. I think I'm still missing something core here... I tried using getComponent() to get the script, but that doesn't work either. Any ideas on what I'm missing here? Thanks!

BTW, this is the GetComponent script I tried.

         Human c = human.GetComponent<Human>();
         c.charName = "gabe";

It showed the properties fine as I typed c. but it still did not set the value when I ran it. I'm doing something wrong...

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 fafase · Dec 20, 2012 at 08:28 AM

This might be a wrong use of static variables. Do you plan on having many Human objects at the same time? If so, static won't do as all of them would get the same unique speed.

 I thought static meant that it was unchangeable

Nope, this is const or readonly. You could have an issue with naming though. From what I see, you are mixing prefab reference and object reference

Human.cs

 public class Human : MonoBehaviour {
     public int speed = 5;
 }

TheOtherScript.cs

 public class NewClass:MonoBehaviour {
     public GameObject prefab;  // Drag prefab here from editor
     GameObject human;
     void Start(){
         human = (GameObject)Instantiate(prefab, new Vector3(0,0,0), Quaternion.identity);
     }
     void Update(){
         if(Input.GetKeyDown(KeyCode.Space)){
             Human script = human.GetComponent<Human>();
             script.speed=10;
         }
     }
 }

Fact is the prefab declaration is a reference to a prefab in the project panel. This is not what you need to access. Also your Human declaration is within the Start, so it gets lost at the end of it. When using human you were talking to the prefab but not the object. Instantiate returns an Object that you need to cast into a GameObject to have access to the functionality of a game object.

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 GabeOfThrones · Dec 20, 2012 at 05:41 PM 0
Share

Thanks, fafase! This was extremely helpful and corrected some things I was thinking about all wrong. One thing I'm still having trouble with is why I shouldn't set my speed, etc in the start(). I basically just want to initialize this... like set it's name, etc. I know there's other ways and places to do that, but is there a reason NOT to do it in the start()?

avatar image fafase · Dec 21, 2012 at 07:13 AM 0
Share

In the Start or as I did is pretty much the same. From what I know that might be wrong, you cannot have an explicit constructor with classes derived from $$anonymous$$onoBehavior. When using

 int variable =10;

you somehow initialize in the constructor that Unity makes. Start is a function that is called. So you can perform some more action like for loops or used values of other variables.All in all not a big difference I guess. Awake and Start also define when you wan tit to happen. To be confirmed

avatar image fafase · Dec 21, 2012 at 07:18 AM 0
Share

@eric5h5 will probably answer that better than me though...

avatar image
-1

Answer by magicqy · Dec 21, 2012 at 12:58 AM

you should Instantiate a GameObeject with a script named "Human" not to Instantiate a script only.

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 Eric5h5 · Dec 21, 2012 at 01:34 AM 1
Share

This is wrong. There's no problem using a script name as a public variable and then instantiating that. All it means is that you drag a prefab which contains that script onto the slot and it instantiates that prefab. So you get the GameObject with the script attached as an instance. In the exact same way that you can specify any other component as a public variable and instantiate it.

avatar image clunk47 · Dec 21, 2012 at 01:37 AM 0
Share

@Eric5h5 +1

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

14 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

Related Questions

Multiple Cars not working 1 Answer

C# How to access a variable in a script from an instance with the same script? 1 Answer

Distribute terrain in zones 3 Answers

I want make a box colleder for top, botton left and roght in c# but dont work 0 Answers

Posting To Twitter From App? - Unity 3 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