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 moggames · Jul 05, 2021 at 08:31 PM · prefabvrspawn

Prefab spawning without running script (uses VR controllers)

Similar to most people on here I'm fairly new to the unity scene and I'm attempting to create a specific mechanism in my game: I want to create a bush where I can spawn fruit items from; the fruit on the bush take a specific amount of time to grow and can't be interacted with until they're fully grown. Once grown as soon as you grab the fruit, it spawns another in the position of the original to repeats the cycle.

However, how the code works is it spawns a first fruit, grows and interacts once grown but once I grab the first fruit, the plant stops growing and just shows the fully grown object(however after every time you grab that fruit it spawns another fruit as intended) ,this object doesn't react with gravity unlike the original which applies gravity after interaction.

The object is a prefab and contains the following script:

using System.Collections; using System.Collections.Generic;

using UnityEngine; using UnityEngine.XR.Interaction.Toolkit;

public class GrowthScript : MonoBehaviour { Collider ObjectCol; [SerializeField] private GameObject Item;

 public int GrowTime = 6;
 public float MinSize = 0.1f;
 public float MaxSize = 1f;
 public float Timer = 0f;
 private XRGrabInteractable grabInteractable = null;

 

 public bool IsMaxSize = false;
 public bool CanRegrow = false;

 // Start is called before the first frame update

 void Start()
 {

     Debug.Log("Growing");
     IsMaxSize = false;
     ObjectCol = GetComponent<Collider>();
     // if the plant isnt full size, it starts a routine to grow
     if (IsMaxSize == false)
     {
         ObjectCol.enabled = !ObjectCol.enabled;
         StartCoroutine(Grow());

     }

 }
 private void Awake()
 {
     grabInteractable = GetComponent<XRGrabInteractable>();
     grabInteractable.onSelectEntered.AddListener(Obtain);
     
 }

 private IEnumerator Grow()
 {
     Vector3 startScale = new Vector3(MinSize, MinSize, MinSize);
     Vector3 maxScale = new Vector3(MaxSize, MaxSize, MaxSize);

     do
     {
         transform.localScale = Vector3.Lerp(startScale, maxScale, Timer / GrowTime);
         Timer += Time.deltaTime;
         yield return null;
     }
     while (Timer < GrowTime);
     IsMaxSize = true;
     CanRegrow = true;
     Debug.Log("Grown");
     ObjectCol.enabled = !ObjectCol.enabled;
 }
 private void Obtain(XRBaseInteractor interactor)
 {
     if (CanRegrow == true)
     {
         
         GameObject instance = Instantiate(Item, transform.position, transform.rotation) as GameObject;
         CanRegrow = false;



     }
 }

}

It would be Deeply appreciated if I could receive help on why the prefab doesn't run the code on respawn or a way to solve the problem.

Much appreciated (Good Luck)

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

Answer by Devster2020 · Jul 06, 2021 at 04:02 PM

I start with indicating that the following lines are almost useless .. you have just started the scene, so you expect the values to be the ones you set 2 lines above..

 if (IsMaxSize == false)
 {
      ObjectCol.enabled = !ObjectCol.enabled;
      StartCoroutine(Grow());
 }


simply remove the if and start directly the coroutine..


Now, your problem comes from the fact that once you have harvested the fruit, you don't reset the "isFull" variable to false and you don't restart the growing method..

to resolve the issue, just use this code:

 private void Obtain(XRBaseInteractor interactor) {
     if (CanRegrow == true) {
         GameObject instance = Instantiate(Item, transform.position, transform.rotation) as GameObject;
         CanRegrow = false;
         IsMaxSize = false;
         ObjectCol.enabled = !ObjectCol.enabled; // I think you want to have ObjectCol.enabled = false;
         StartCoroutine(Grow()); // Restarting the growing method
     }
 }
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 moggames · Jul 07, 2021 at 09:40 AM 0
Share

Thank you for the response :) i tried the code that you used but unfortunately what happened was that every time I grab the fruit it would spawn a brand new one(imagine it like an instant clone). However I found the solution to the fruit not growing, being that the fruits growth is based on a time which I just needed to reset to 0.

  private void Obtain(XRBaseInteractor interactor)
     {
         if (CanRegrow == true)
         {
             Timer = 0f;
             Instantiate(Item, transform.position, transform.rotation);
             CanRegrow = false;    
         }

So thank you for helping me get to the $$anonymous$$dset to get to the solution :) I deeply appreciate the assistance!!

avatar image Devster2020 moggames · Jul 07, 2021 at 03:23 PM 0
Share

You're welcome :)

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

198 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

Related Questions

Where did the avatars go? Multiplayer spawn prefabs 0 Answers

Give prefab a parent 2 Answers

How can I make an in-game search engine similar to Scribblenauts 1 Answer

i need to destroy prefab clones 1 Answer

Use prefab as particle in particles system 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