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 justice_chasmo · Mar 08, 2018 at 07:41 PM · referencing

why doesn't my reference work?

hi. i have a problem with my references. when i reference an object in the game (Bf) the code works. but when i reference a prefab which i have an instance of the code does not work. what am i missing? Card is the reference to the prefab. and yes both has the referenced script (DropZone)

the code is:

public GameObject Bf; public GameObject Card;

   void EnemyTurn()
     {
         Debug.Log("Enemy's turn");
        
        
             Bf.transform.GetComponent<DropZone>().typeOfItem = Draggable.Slot.NotActive;
             Card.transform.GetComponent<DropZone>().typeOfItem = Draggable.Slot.NotActive;
 
         //turn = true; //make an if statement that asks if movement = 0 and AP = 0 then change turn status to 1
     }

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 pako · Mar 08, 2018 at 08:19 PM

How do you instantiate the prefab?

This won't work, because it doesn't refer to the instance of the prefab, but to the prefab asset itself, which is not instantiated:

 //Card refers to the prefab asset, not an instance of it
 Card.transform.GetComponent<DropZone>().typeOfItem = Draggable.Slot.NotActive;
 
 

This would work:

 //myCard refers to an instance of the Card prefab
 myCard = Instantiate(Card, transform.position, transform.rotation); //for example
 
 //...then
 myCard.transform.GetComponent<DropZone>().typeOfItem = Draggable.Slot.NotActive;

Comment
Add comment · Show 19 · 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 justice_chasmo · Mar 08, 2018 at 10:23 PM 0
Share

first of all thank you for your time and help. i have a couple of objects in the game world derived from the prefab, but as you say i try to reference the prefab so that all instances of the prefab would be affected.

i'm not sure when i'm suppose to use the myCard = Instantiate(Card, transform.position, transform.rotation);

since it throws the error of not being able to implicitly convert from Object to GameObject. then i thought ok what if i just call it as an object ins$$anonymous$$d. but the mess with the transform in the second line. `

avatar image pako justice_chasmo · Mar 08, 2018 at 11:24 PM 0
Share

i'm not sure when i'm suppose to use the myCard = Instantiate(Card, transform.position, transform.rotation);

This is why the first thing I asked you was:

How do you instantiate the prefab?

...and I clearly pointed out:

...//for example

In other words, the information you provided is insufficient to answer the question fully, and I did my best to provide you with useful information, together with an example, knowing that my example is a pointer to the usage, and should not be used "as is", because the exact implementation depends on the remaining part of your code, which you don't show.

avatar image justice_chasmo pako · Mar 09, 2018 at 12:17 AM 0
Share

you did provide very useful information but i'm trying to wrap my head around it. The way you explain it makes sense but i'm just not seeing the full picture i guess. the full script is as follows`using UnityEngine; using System.Collections; using UnityEngine.UI;

public class Turn$$anonymous$$anager : $$anonymous$$onoBehaviour {

 public bool turn = true;
 public GameObject Bf;
 public GameObject Card;
 public GameObject $$anonymous$$$$anonymous$$;


 // Use this for initialization
 void Start ()
 {
     //Card = Instantiate (Card, transform.position, transform.rotation);
 }
 
 // Update is called once per frame
 void Update ()
 {
     if (turn == true)
     {
         PlayerTurn();
     }
     else
     {
         EnemyTurn();
     }

    
     


 }


 IEnumerator timer()
 {
     Debug.Log("timer started");
     
     yield return new WaitForSeconds(5f);
     

     Debug.Log("Timer stopped");
     TurnChange();
     
 }


 void PlayerTurn()
 {
     StartCoroutine("timer");
     Debug.Log("Player's turn");
     //turn = false; //make an if statement that asks if movement = 0 and AP = 0 or if the "end turn" button was pressed to change turn status to 2
 }

 void EnemyTurn()
 {
     StartCoroutine("timer");
     Debug.Log("Enemy's turn");
     



     Bf.transform.GetComponent<DropZone>().typeOfItem = Draggable.Slot.NotActive;
     //Card.transform.GetComponent<DropZone>().typeOfItem = Draggable.Slot.NotActive;

     //turn = true; //make an if statement that asks if movement = 0 and AP = 0 then change turn status to 1
 }


 public void TurnChange()
 {
     if (turn == true)
     {
         turn = false;
         StopCoroutine("timer");
     }
     else
     {
         turn = true;
         StopCoroutine("timer");
         $$anonymous$$$$anonymous$$.transform.GetComponent<$$anonymous$$ana>().Add$$anonymous$$ax(1);
         $$anonymous$$$$anonymous$$.transform.GetComponent<$$anonymous$$ana>().Refill$$anonymous$$ana();
     }
 
 }

 }

` the Card GameObject is the prefab itself. and i see from your answer i never put in an actual instantiation but it is because i don't know when i am suppose to do that. if you need the DropZone script aswell i'll provide that too

Show more comments
Show more comments
avatar image pako justice_chasmo · Mar 08, 2018 at 11:49 PM 0
Share

Additionally:

i try to reference the prefab so that all instances of the prefab would be affected

You can't affect all instances of the prefab at runtime, this is why you must have a reference to the specific prefab instances at runtime, in order to change them individually.

avatar image justice_chasmo pako · Mar 09, 2018 at 10:07 PM 0
Share

Pako i just want to say thank you so much!! you just broke a $$anonymous$$AJOR obstical i had no idea how to get past! so again thank you so much!

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

74 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

Related Questions

Animation scripting 1 Answer

Can't activate my timer for shooting script? 2 Answers

Assign EventTrigger reference Script at Runtime 1 Answer

[Probably an easy question]Troubles saving my GameObject through scenes 1 Answer

A problem regarding accesing a variable in other script. 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