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 Prasenjit_LinuxGuy · May 11, 2016 at 09:54 AM · c#scripting problemsetactivelevel select

Regarding SetActive function

In my game . I have added collectables , I have added a collectable naming "PickUp3" . I want it to work such that as the player touches it , the path to Level 2 naming "level2path" becomes active . Here is my script but I don't know why it isn't working . Help please .

Transcript :

 using UnityEngine;
 using System.Collections;
 
 public class lev2 : MonoBehaviour {
     
     void Start ()
     {
     
     }
     void OnTriggerEnter(Collider col)
     {
         if (col.gameObject.name == ("PickUp3")) 
         {
             {
                 other.gameObject.name==("level2path")
                 {
                     other.gameObject.SetActive (true);
                 }
             }
         }
     }
     void Update () {
     
     }
 }

Comment
Add comment · Show 3
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 Tyche10 · May 12, 2016 at 06:33 PM 0
Share

Does one of them have a rigidbody attached? Where does the variable 'other' come from in your code?

avatar image Le-Pampelmuse · May 12, 2016 at 07:58 PM 2
Share

That code will not even compile because:

  1.    if (col.gameObject.name == ("PickUp3")) 
          {
              { <== Error 1
                  other.gameObject.name==("level2path") <== Error 2 & Error 3
                  {
                      other.gameObject.SetActive (true); <== Error 3
                  }
              } <== Error 1
          }
    

Error 1: why are there extra brackets without any meaning to them?
Error 2: what is this line?? Did you forget to put this into an if statement?
Error 3: other is not declared anywhere, the script suddenly sees other.gameObject.name but it never was told what other is.

I suggest you look at the basic beginner coding tutorials provided by Unity, once you get comfortable writing code, you can try to do your own stuff. Don't start with things you don't understand ;)

Also you seem to misunderstand the OnTrigger functions, they work only for the object the script is applied to. Again, take a step back, learn from the beginning, then gradually improve the complexity of your coding.

You can find the many official tutorials in the Learn Section.

Have nice day.

avatar image Tyche10 Le-Pampelmuse · May 12, 2016 at 08:10 PM 0
Share

I thought, let's start with the other variable ;)

1 Reply

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

Answer by TenebrisMusic · May 12, 2016 at 07:57 PM

There are several things that can be the issue here.

First of all, to what object is this script attached? You would need this script to be on the player for it to pass the if-statement.

Second, does one of the objects (the player or the pickup object) have a rigidbody attached? And have you checked one of the colliders to be a trigger? If not then the OnTriggerEnter method will not be called.

Third, you are calling upon the variable 'other', but you don't seem to have defined it for as far as I can see. Because of this it will not do anything with your object.

If I were you I would assign this script to the pick-up object instead of the player object, and check for a collision with the player to initiate the reaction. This would probably look something like this:

 using UnityEngine;
 using System.Collections;
 
 public class Pickup3Script : MonoBehaviour {
     public GameObject level2path;
 
     void OnTriggerEnter (Collider col) {
         if (col.gameObject.tag == "Player") {
             level2path.SetActive (true);
         }
     }
 }

In this case you would have tagged your player object with the standard tag "Player", and you would have to drag the level2path object into the inspector. An alternative to having to insert it in the inspector would be to replace

             level2path.SetActive (true);

with

             GameObject.Find("level2path").SetActive (true);

In this case the path object should be named level2path.

Hope this helps.

Cheers!

  • Ruben

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 Le-Pampelmuse · May 12, 2016 at 08:09 PM 1
Share

you are calling upon the variable 'other', but you don't seem to have defined it for as far as I can see. Because of this it will not do anything with your object.

It will not only do nothing with the object, it creates a compiler error which makes it impossible to playtest. ;)

avatar image Prasenjit_LinuxGuy · May 13, 2016 at 02:33 PM 0
Share

How can I define the variable "other" . Only that's the main problem

avatar image Le-Pampelmuse Prasenjit_LinuxGuy · May 13, 2016 at 05:12 PM 2
Share

http://unity3d.com/learn/tutorials/modules/beginner/scripting/variables-and-functions?playlist=17117

Like I said, watch the beginner scripting tutorials from 1-28 that should clear all your starting problems.

If you don't know how to declare a new variable yet, there is no sense in continuing, do you understand? ;) Learn the basics, then try advanced stuff. It's not hard, the tutorials are a very good source of information.

avatar image TenebrisMusic Prasenjit_LinuxGuy · May 14, 2016 at 07:22 AM 1
Share

Like Le Pampelmuse said, it would be wise to first follow the basic tutorials. It will spare you a lot of time in the end as you won't have to turn to the community and wait for answers that can be found rather easily.

There are a great deal of tutorials on the official Unity Tutorial page you'll find useful. I'd recommend following some of the scripting tutorials first, but I - as a sometimes impatient person myself - can understand you just want to begin your game. I'd try finding a tutorial that gets closest to your game-concept, so you'll be able to continue on what you've build for the tutorial when you feel like you understand enough.

Cheers!

avatar image Le-Pampelmuse TenebrisMusic · May 14, 2016 at 06:41 PM 2
Share

"It will spare you a lot of time in the end as you won't have to turn to the community and wait for answers that can be found rather easily."

$$anonymous$$ore importantly, it is not the purpose of UA to explain things that are publicly available to read. ;)

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

level manger and starting scene will not work together 0 Answers

How to enable script on another gameobject in C#? 1 Answer

Game object not enabled after SetActive(true). 1 Answer

How to tell if the code flow changed 2 Answers

Level multiple data values should I use a list? 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