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 /
avatar image
0
Question by death-be-upon-me · Jan 27, 2017 at 04:42 PM · c#scripting problemcomponent

Why are the components on my script disappearing when I press play

When I press the play button the components I have become unlinked from the script they are supposed to be attached to (see the Create New Character Script)

alt text

alt text

Anyone know what's happening or how to fix it?

unity-issue-2.jpg (230.8 kB)
unity-issue-1.jpg (248.9 kB)
Comment
Add comment · Show 6
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 Chasbas_80 · Jan 27, 2017 at 06:37 PM 0
Share

@death-be-upon-me, can you post your script so I can see what's going on? Also, are there any other scripts that are accessing your CreateNewCharacter script? $$anonymous$$aybe your BattleSystem or Player scripts?

avatar image death-be-upon-me Chasbas_80 · Jan 30, 2017 at 09:50 PM 0
Share
     using System.Collections;
     using System.Collections.Generic;
     using UnityEngine;
     using UnityEngine.UI;
     
     public class CreateNewCharacter : $$anonymous$$onoBehaviour {
     
         private BasePlayer newPlayer;
         private string playerName;
         private string playerGender;
         private bool is$$anonymous$$elee = false;
         private bool isSpeed = false;
         private bool isRanged = false;
         private bool isSecret = false;
         private bool is$$anonymous$$ale = false;
     
         public InputField getName;
         public GameObject maleCharacterPic;
         public GameObject femaleCharacterPic;
         public Text toggleGenderText;
     
         void Awake()
         {
             getName = GetComponent<InputField>();
             maleCharacterPic = GetComponent<GameObject>();
             femaleCharacterPic = GetComponent<GameObject>();
             toggleGenderText = GetComponent<Text>();
         }
     
         void Update()
         {
             if (is$$anonymous$$ale == false)
             {
                 femaleCharacterPic.SetActive(true);
                 maleCharacterPic.SetActive(false);
             }
             else
             {
                 maleCharacterPic.SetActive(true);
                 femaleCharacterPic.SetActive(false);
             }
         }
         
         public void ToggleGender()
         {
             if (is$$anonymous$$ale == true)
             {
                 is$$anonymous$$ale = false;
                 toggleGenderText.text = "Female";
             }
             else if (is$$anonymous$$ale == false)
             {
                 is$$anonymous$$ale = true;
                 toggleGenderText.text = "$$anonymous$$ale";
             }
         }
 

avatar image death-be-upon-me Chasbas_80 · Jan 31, 2017 at 08:47 AM 0
Share

@Chasbas_80 I've changed the Awake() to Start() and tried putting the GetComponent parts in different voids but that didn't work

avatar image tanoshimi · Jan 27, 2017 at 07:07 PM 0
Share

Are you initialising those variables in Start(), perhaps?

avatar image death-be-upon-me tanoshimi · Jan 30, 2017 at 09:45 PM 0
Share

I've tried both Start() and Awake()

avatar image yogee · Jan 31, 2017 at 09:19 AM 0
Share

you assigned in the editor , why are u reassign in script

1 Reply

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

Answer by yogee · Jan 31, 2017 at 09:22 AM

try like this:

  maleCharacterPic = transform.FindChild("CharacterPicMale").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 death-be-upon-me · Feb 01, 2017 at 02:24 PM 0
Share

Thanks dude I've used this like you said for the make and female pics but I changed it a little for the other two.

 void Start()
     {
         getName = transform.FindChild("InputField").GetComponent<InputField>();
         toggleGenderText = transform.FindChild("ToggleGenderText").GetComponent<Text>();
         maleCharacterPic = transform.FindChild("CharacterPic$$anonymous$$ale").gameObject;
         femaleCharacterPic = transform.FindChild("CharacterPicFemale").gameObject;
     }
avatar image Bunny83 death-be-upon-me · Feb 01, 2017 at 02:35 PM 0
Share

Right, "GameObject" is not a component. So the line

 GetComponent<GameObject>();

makes no sense. Why do you actually reassign those references manually via script? If you have assigned them in the editor you don't need all this. In fact if you want to assign them via script you should make your variables private as they don't need to show up in the inspector. This is bad design as fields exposed to the inspector are ment to be set inside the editor. However since you always overwrite the values in Start it's pointless to set them in the inspector.

avatar image death-be-upon-me Bunny83 · Feb 02, 2017 at 07:17 PM 0
Share

@Bunny83 I made them private and it just gives me a NullReferenceException on all 4 of the variables

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

10 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

Related Questions

Enabling multiple Monobehaviour Components in a game object 1 Answer

Inheritance and Component Types in C# 2 Answers

Access water4 scripts via my script? 1 Answer

Multiple Cars not working 1 Answer

Distribute terrain in zones 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