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 Avanak · Sep 26, 2014 at 08:08 PM · errorgameobjectvariable

How to change variables on a new gameobject's script

So, I have a MapBuilder object that has a script which places random stars on the 2d plane. While generating each star, I want to give them some variables to make them unique, for example their color or size.

I made a script that gets added to the new star objects that stores those variables, but I need to change them from the MapBuilder script.

The stars start as a new empty GameObject. I then add the script via the AddComponent function. Here is where I encounter my problems: I try to change the variable of the scripts, but Unity returns an error: starColor is not a member of UnityEngine.Component.

I have seen other people sing this method for chnaging variables so i don't know why it is not working.


This is the script in the MapBuilder object:

 #pragma strict
 
 var amtStars = 1;
 
 function Start () {
     
     for (var  i = 0; i < amtStars; i++){
     
         var star = new GameObject("Star" + (i + 1));
         var inst = GameObject.Find("Star" + (i + 1));
         
         inst.AddComponent("starControler");
         var script = inst.GetComponent("starControler");
         script.starColor = "red";
         //star.transform.position = transform.position;
         //star.AddComponent("SpriteRenderer");
     }
 }


This is the script in the new star, it is just there to store the variable:

 #pragma strict
 
 public var starColor : String;
 
 function Start(){
     Debug.Log(starColor);
 }
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

1 Reply

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

Answer by Baste · Sep 26, 2014 at 08:20 PM

You need to tell your script that the component you get is a starControler:

 inst.AddComponent("starControler");
 var script = inst.GetComponent("starControler") as starControler;
 script.starColor = "red";

Since AddComponent actually returns the added component, you can cut it down and make it more readable:

 var script = inst.AddComponent("starControler") as starControler;
 script.starColor = "red";

That should work, though my UnityScript isn't the sharpest. As an additional pet peeve - controller is written with two l's, and classes should be capitalized, so it should be StarController, not starControler.

Comment
Add comment · Show 5 · 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 Baste · Sep 26, 2014 at 08:23 PM 0
Share

By the way, I'm really not very good at UnityScript. If that doesn't work, try using the GetComponent method like they do in the docs, here.

avatar image Avanak · Sep 26, 2014 at 08:26 PM 0
Share

Thanks for your input, but the method you proposed does not work for me, i now get the error: The name 'starControler' does not denote a valid type ('not found').

Also the GetComponent method gave me the same error as before

avatar image Baste · Sep 26, 2014 at 08:59 PM 0
Share

The "not a valid type" error is because the compiler can't find a type named "starControler". Replace it with the actual name of the script you're adding.

avatar image Avanak · Sep 26, 2014 at 09:21 PM 0
Share

Ah thanks, i made a typo. Stupid me! Anyway it works, many thanks!

avatar image Kiwasi · Sep 26, 2014 at 09:25 PM 0
Share

That's why it's better to use generics ins$$anonymous$$d of the string form in just about all cases.

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

how can I display a variable as a GUIText 5 Answers

Scripting error! 1 Answer

Using variable from one script attached to one object in another script attached to another object 0 Answers

How can I access a variable from another script in a separate object? 1 Answer

How to access gameObject variable script 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