Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by xiaotianhu · Aug 19, 2016 at 11:46 AM · inspector

assign variable by inspector failed...

Hi guys,not good at english, but really need some help. I'm learning with the book http://unitybook.net/,and got stuck.Searched for a day but nothing helps.

so here is my question: I have a generator(like a box) in Scene,and I want to change it's texture by script(JS).

?alt text

my script is on the camera,which is the FirstPersionCharacter.Part of it is this:

var collectImage:Texture2D[];
public var collectBox:Renderer;
...
function CellPickup() {
charge ++;
collectBox.material.mainTexture = collectImage[charge];
Debug.Log(collectBox);
}

When the function CellPickup() is called,I want to change the collectBox's texture.the collectBox variable represents chargeMeter1 ,so I assigned this variable by inspector ,by drag the chargeMeter1 from Hierarchy.

?alt text

Then I Run and play,When the function CellPickup() is called,something wrong happen:

UnassignedReferenceException: The variable collectBox of playerCollisions has not been assigned.

You probably need to assign the collectBox variable of the playerCollisions script in the inspector. But I have already assigned the collectBox variable by inspector.So Please ,anything will be helpful.Thank you. [1]: /storage/temp/76420-j1.jpg [2]: /storage/temp/76422-j2.jpg
j2.jpg (332.1 kB)
j1.jpg (219.5 kB)
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
0
Best Answer

Answer by xiaotianhu · Aug 22, 2016 at 04:28 AM

Thanks @ScaniX, And I have solved this.

I do get the reference like this:
var cb:GameObject;
cb = GameObject.Find("chargeMeter1");
collectBox = cb.GetComponent(Renderer);

The GameObject is the pack of all objects in Unity,and one object can have many components on it,is that right?

Then I also found something else,which may cause the problem.

Science I use the First Controller Camera,I gets two things in my Hierarchy:
Parent is FPSController,and Child is FirstPersonCharacter.
I'v put my script compoment on both of these,and then I only remember to change the variable with the script on the child FirstPersonCharacter.So when I Debug in the function Start(), each time I'll get two results.Then after I tried several minutes,I found only the script on parent FPSController will work. Then I do
collectBox.material.mainTexture = collectImage[charge];
Everything goes right.

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 ScaniX · Aug 19, 2016 at 11:52 AM

As there is an "apply" button, I guess those objects are prefabs.

You cannot assign an actual scene instance to a prefab. That will be gone when you instantiate the prefab again. At least I experienced that behaviour in my scenes and it kinda makes sense. I don't think you can assign a member from one prefab to another one either.

You will have to assign the reference in the Start() method by using GameObject.Find("chargeMeter1") in case of dynamically created things.

Comment
Add comment · Show 1 · 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 xiaotianhu · Aug 22, 2016 at 02:59 AM 0
Share

If I get the reference by using GameObject.Find("charge$$anonymous$$eter1"),then I get a instance of GameObject,not a instance of Renderer,so when I do collectBox.material.mainTexture ,it will fail with "material' is not a member of 'UnityEngine.GameObject".So what should I do? PS:I tried with GameObject.Find("charge$$anonymous$$eter1") as Renderer but I got Null.

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

67 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 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 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 avatar image avatar image avatar image avatar image

Related Questions

Why i can't see scenes in inspector? 0 Answers

How do I use EditorGUILayout.EnumPopup with an enum with 'holes' in a custom inspector. 3 Answers

Unassigned Reference Exception on an Animator, but Animator is assigned in Inspector? 0 Answers

Unity 4.1.5 is not showing certain public variables in the inspector 1 Answer

Inspector List: DropDown list Update regarding Prefab in asset folder 1 Answer


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