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 · Feb 08, 2016 at 05:21 PM · gameobjectsstatictoggle

toggling between prefabs

Hi I'm trying to switch between prefabs using a toggle switch. I've set up already five balls turning on and off at the click of a toggle. The trick (and or problem) is having the newly selected ball appear on screen at the same place as the previous ball. Previously working in Unity 4.3.x I had available to me a method from EventSystems called lastSelectedGameObject, which I thought that would help me identify what the previous ball was and place the new ball at with Transform.position. The code that I have written for each of the balls in the Start() method is as follows:

 public void Start(){
 
         // Unlocking AirBall
         if (sumBalls == 3) {
             aToggle.interactable = true;
             aBall.SetActive (true);
 
             // Treasure Unlocking animation
         }
         
         if (aBall.activeSelf) {
             
             aBall.GetComponent<Renderer>().enabled = true;
             lastBall = EventSystem.current.lastSelectedGameObject;
             lastBallPos = new Vector3 (lastBall.transform.position.x, lastBall.transform.position.y, lastBall.transform.position.z);
             aBall.transform.position = lastBallPos;
         }
         else {
             
             aBall.SetActive(false);
             aBall.GetComponent<Renderer>().enabled = false;
         }
 
 
 
         }

However, when I've upgraded to Unity 5.3.2, there's a warning in the console about that method being obsolete. My next question regarding this same code is under the "Unlocking AirBall" comment. In this small code, the variable sumBalls is in the code a static int variable because what I was trying to do is calculate the number of 'hit' (also a static variable) from other classes like this:

 private static int sumBalls;
 
     public void Awake (){
     
         aBall = GameObject.FindWithTag ("AirBall");
         lastBall = new GameObject ();
         aBall.SetActive (false);
         aToggle = aBall.GetComponent<Toggle> ();
         aToggle.interactable = false;
         sumBalls = WaterBall.hit + FireBall.hit + LandBall.hit + BowlingBall.hit;
         Debug.Log ("Number of hits (AirBall): " + sumBalls.ToString ());
     }

I just hope that I'm doing it the right way.

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

Answer by Owen-Reynolds · Feb 08, 2016 at 05:51 PM

Is that Start code for a newly Instantiate ball? If so, here's the normal way:

When you Instantiate an object, keep the pointer to it and use that to set it up. You can read more about how to find another object's script, etc. But some examples:

 GameObject gg = Instantiate(newBallPF);
 // move it there yourself:
 gg.transform.position = ...
 // or tell it's script about where it came from:
 gg.GetComponent<BallScript>().oldBall = myOldBall;
 gg.GetComponent<BallScript>().ballNum = currentBallNum;

But, are you making more and more balls? If you have 5 balls total, it seems easier just to have them all, and hide and move them as needed.

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

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

When attaching a static game object or singleton script to a game object in a current scene, how can I keep the static object attached to the original game object without it being removed after switching scenes? 1 Answer

Referencing components of multiple game objects within an array 0 Answers

Create 2 seperate instances of the same prefab,create 2 Separate instances of the same prefab 0 Answers

What are some best practices for static variables in multiplayer? 0 Answers

Object reference not set to an instance of an object 0 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