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 jjplay175 · Aug 24, 2014 at 11:05 AM · c#gameobjectrendererdisable

Disable any objects through 1 script

Hi I made clones of a ghost around my map and they flicker over time but I want it so after 11-15 flickers that the ghost will disappear and it randomly picks a clone and enables that instead

             GameObject ScaryFemale = GameObject.Find("ScaryFemale");
             GameObject ScaryFemale1 = GameObject.Find("ScaryFemale1");
             GameObject ScaryFemale2 = GameObject.Find("ScaryFemale2");
         }



     while (ScareSel <= 1){
             if(invisloop <= 11) {
                 Debug.Log (invisloop);
             if (gameObject.renderer.enabled) {
                 curTime = Random.Range(0.01f, maxTimeInvis);
             } else {
                 curTime = Random.Range(0.01f, maxTimeVis);
                 invisloop++;
             }
             gameObject.renderer.enabled = !gameObject.renderer.enabled;
             yield return new WaitForSeconds(curTime);
             }


There are 3 objects so how do I get the GameObject "ScaryFemale" in this code instead of the gameObject.renderer.enabled?

I had this working as 4 scripts by placing 3 on the character and having 1 control which script was running but it really wasn't that feasible

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
1
Best Answer

Answer by Alessio89 · Aug 24, 2014 at 11:41 AM

As we can see from your snippet, it seems that the three GameObjects are inside some brackets, making them valid only inside those brackets. You should instead put them either outside any method and make them public, so you can access them from everywhere, or declare them outside those brackets (I don't know if there is this possibility, your snippet doesn't include much).

 public class Script : MonoBehaviour
 {
     // I've delcared them outside any method. I can now access them from anywhere in this class.
        GameObject ScaryFemale;
        GameObject ScaryFemale2;
        void Start()
        {
            // Let's fill in the objects when the game starts
            ScaryFemale = GameObject.Find("ScaryFemale");
            ScaryFemale2 = GameObject.Find("ScaryFemale2");
        }
     
        void Update()
        {
           //I Can now access both scaryfemales from here
           ScaryFemale.transform.position = Vector3.zero;
           ScaryFemale2.renderer.enabled = false;
        }
 }

(This code is just a sort of pseudo-code, you should not be copying this, instead look at what I meant and try to implement it in your own script :))

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 jjplay175 · Aug 24, 2014 at 12:42 PM 0
Share

no you are completely right and I feel stupid now... I've been working on my code all night and I simply messed this one up, thanks for the help! :)

avatar image Alessio89 · Aug 24, 2014 at 12:59 PM 0
Share

eheh, it happens to the best of us :) it can be tiresome at time to do these stuff! I'm glad you figured it out. Have fun with your poject! :)

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

error CS0131: The left-hand side of an assignment must be a variable, a property or an indexer 3 Answers

Load a GameObject that is outside of script and set it active at the same time 1 Answer

GameObject.setActive(bool) is not activating my gameObject 1 Answer

Networkly disabaling gameobject? 0 Answers

How do I assign a renderer to a game object? 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