• 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 macca7000 · Aug 28, 2020 at 11:43 PM · getcomponentdropdown

GetComponent has no error but not working

Hi, I'm still fairly new to this and I've been stuck on this problem for a while. I have a drop down list that creates a variable on one script, then on the next script I use GetComponent to carry it over.
This is the first script.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 
 public class ShopSelector : MonoBehaviour
 {
 
     public GameObject shop;
 
     public GameObject oniellTower;
     public Dropdown oniellDropdown;
 
     [HideInInspector] public int gunInt;
 
     void Start()   // Start is called before the first frame update
     {
         OniellDropDown(0);
     }
         
     void Update()  // Update is called once per frame
     {
         
     }
 
     public void SelectOniellTower()
     {
         oniellTower.SetActive(!oniellTower.activeSelf);
     }
 
     public void OniellDropDown(int val)
     {
         if(val == 0)
         {
             gunInt = 1;
             Debug.Log("ShopSelector " + gunInt);
         }
 
         if (val == 1)
         {
             gunInt = 2;
             Debug.Log("ShopSelector " + gunInt);
         }
 
         if (val == 2)
         {
             gunInt = 3;
             Debug.Log("ShopSelector " + gunInt);
         }
     }
 }
 

This is the second script

 using System;
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class HumanTurrets : MonoBehaviour
 {
     private ShopSelector shopSelector;
     public GameObject shop;
   
     [HideInInspector] public Transform target;
     private Enemy targetEnemy;
     public float range = 10f;
     public string enemyTag = "Enemy";
     public float fireRate = 1f;
     private float fireCountdown = 0f;
     public Transform partToRotate;
     public float turnSpeed = 5f;
     public GameObject bulletPrefab;
     public Transform firePoint;
     [HideInInspector] public int gunToUse;
     public GameObject pistol;
     public GameObject p90;
     public GameObject sniper;
 
     
     void Awake()
     {
         gunToUse = shop.GetComponent<ShopSelector>().gunInt;
         Debug.Log(gunToUse);
      }
     
     void Start()  // Start is called before the first frame update
     {
       
 /*
         if(shopSelector.gunInt == 1) { gunToUse = 1; }
         else if (shopSelector.gunInt == 2) { gunToUse = 2; }
         else if (shopSelector.gunInt == 3) { gunToUse = 3; }
 
 */
         if (gunToUse == 1) { pistol.SetActive(!pistol.activeSelf) ; }
         else
             if (gunToUse == 2) { p90.SetActive(!p90.activeSelf); }
         else
             if (gunToUse == 3) { sniper.SetActive(!sniper.activeSelf); }
         
        
         Debug.Log(gunToUse);
 
 
         InvokeRepeating("UpdateTarget", 0f, 0.5f);
     }
 
     
    
 
 }
 


I don't get any errors but the debug.logs give me a 0 (which I assume is the default Integer value). I have used GetComponent before in exactly the same way and it has worked fine, I don't understand what I am doing wrong on this one.

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 macca7000 · Sep 03, 2020 at 04:08 AM

I was trying to reference an in-scene object via an inspector field on the turret prefab, which doesn't work.
What I don't understand is I made the UI object a prefab aswell so it should have worked,but anyway.
I've changed the scripts around a bit and it works now.
This is what I changed in the first script

 public void OniellDropDown(int val)
     {
         if(val == 0)
         {
            oniellPrefab.GunToUse(1);
         }
 
         if (val == 1)
         {
             oniellPrefab.GunToUse(2);
         }
 
         if (val == 2)
         {
             oniellPrefab.GunToUse(3);
         }
     }

This is what I changed in the second script

 public void GunToUse(int val)
     {
         foreach (GameObject obj in weapons)
         {
             obj.SetActive(false);
         }
         if (val == 1)
         {
             pistol.SetActive(true);
         }
 
         if (val == 2)
         {
             p90.SetActive(true);
         }
 
         if (val == 3)
         {
             sniper.SetActive(true);
         }
     }

No need to use GetComponent at all.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

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

Related Questions

Javascript GetComponent not work! 0 Answers

"NullReferenceException: Object reference not set to an instance of an object " only for one component 2 Answers

Disable function in another script 1 Answer

Tag control 0 Answers

How can i get component as an abstract base class? 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